問題描述
當我看到 這個問題時,我想如果可以編寫 jQuery compiler 將會很有幫助.現在,編譯器是指接收 jQuery 代碼并輸出最終執行的原始 javascript 代碼的東西.
When I saw this question I thought it would be helpful if a jQuery compiler could be written. Now, by compiler, I mean something that takes in jQuery code and outputs raw javascript code that is ultimately executed.
這就是我對 jQuery 代碼執行塊的設想:
This is how I vision a block of jQuery code execution:
- 調用一個 jQuery 函數并將參數傳遞給它
- 該函數調用一個原始的 javascript 函數并將它收到的參數傳遞給它
- 新調用的函數執行預期的操作
我知道這是一個非常簡化的模型,它可能要復雜得多,但我認為復雜性降低到重復第 2 步和第 3 步,并調用不同的原始 js 函數,并且每個使用所有或部分參數/先前結果的時間.
I understand that this is a very simplified model and it could be much more complex, but I think the complexity is reduced to steps 2 and 3 being repeated with different raw js functions being called and each time fed with all or a subset of parameters / previous results.
如果我們訂閱該模型,那么我們可能會想出一些方法來讓 jQuery 函數執行雙重任務:
If we subscribe to that model, then we might come up with methods to make the jQuery functions perform double-duty:
- 他們已經做了什么
- 以
raw_function(passed_pa??rams)
的形式記錄他們所做的事情
- What they already do
- Logging what they did in form of
raw_function(passed_params)
我是否做出了一些錯誤的假設,使這成為不可能?任何想法 Firebug 的探查器如何嘗試獲取函數名稱?可以用在這里嗎?
Am I making some wrong assumptions that would make this impossible? Any ideas how Firebug's profiler attempts to get function names? Could it be used here?
編輯
我的想法是制作一個輸入/輸出為的黑盒:
What I was thinking was making a black box with input / output as:
普通 jquery 代碼
→ [BB] → 如果你不使用庫你會寫的代碼
- 我稱它為編譯器,因為您編譯一次,然后就會使用生成的代碼.
- 我認為它至少可以用于教育,也可能有其他用途.
- 人們說這會占用少量代碼并輸出大量代碼;據我所知,這并沒有違背預期的目的
- 人們說我會在頁面渲染中添加一個額外的、不必要的步驟,鑒于最終只會使用生成的代碼(并且可能僅用于學習),這是不正確的.
- 人們說javascript函數和jquery函數之間沒有一對一的關系,并暗示這樣的轉換器太復雜了,可能不值得努力.我現在同意這一點.
- I called this a compiler, because you compiled once and then would use the resulting code.
- I argued that it could have at least educational use, and probably other uses as well.
- People said this would take in a small amount of code and output a huge mass; that does not defy the intended purpose as far as I see
- People said I'd be adding an extra, needless step to page rendering, which, given only the resulting code would ultimately be used (and probably be used just for studying), is not correct.
- People said there is no one-to-one relation between javascript functions and jquery functions, and implied such a converter would be too complicated and probably not worth the effort. With this I now agree.
謝謝大家!
推薦答案
我想你的意思是:如果你寫
I think what you mean is: if you write
var myId = $("#myId")
它將被轉換為
var myId = document.getElementById("myId")
我認為可能,但問題是,jQuery 函數返回 jQuery 對象,所以在上面的例子中,第一個 myId 將是一個 jQuery 對象 &第二個將是一個節點對象(我認為),它將影響編譯后在代碼中稍后需要使用它的其他功能.特別是如果他們被鎖住了
I think its possible, but the problem is, jQuery functions return jQuery objects, so in the above example, the first myId will be a jQuery object & the second will be a node object(i think) which will affect other functions that needs to use it later in the code after compilation. Especially if they are chained
其次,您必須確保轉換確實具有性能優勢.但是,如果您了解所有這些并且可以相應地計劃您的代碼,我認為這是可能的
secondly you will have to be sure that the conversion actually has performance benefits. However if you are aware of all this and you can plan you code accordingly, i think it will be possible
這篇關于Jquery *編譯器*可能嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!