Jquery *编译器*可能吗?

当我看到 这个问题时,我想如果可以编写 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:

  1. 调用一个 jQuery 函数并将参数传递给它
  2. 该函数调用一个原始的 javascript 函数并将它收到的参数传递给它
  3. 新调用的函数执行预期的操作

我知道这是一个非常简化的模型,它可能要复杂得多,但我认为复杂性降低到重复第 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:

  1. 他们已经做了什么
  2. raw_function(passed_pa​​rams)
  3. 的形式记录他们所做的事情
  1. What they already do
  2. 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

相关文章