是Jquery *编译器可能吗? [英] Is Jquery *compiler* possible?

查看:208
本文介绍了是Jquery *编译器可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到这个问题时,我认为如果可以编写jQuery 编译器 将是有帮助的。现在,通过编译器,我的意思是接受jQuery代码,并输出最终执行的原始javascript代码。



一个jQuery代码执行块:


  1. 调用jQuery函数并传递参数

  2. 该函数调用一个原始javascript函数并将接收到的参数传递给它

  3. 新调用的函数执行预期的操作

我理解这是一个非常简单的模型,它可以更复杂,但我认为复杂性减少到步骤2和3用不同的原始js重复函数被调用,并且每次都有参数/以前的结果的全部或子集。



如果我们订阅了那个模型,那么我们可能会想出一些方法jQuery函数执行双重任务:


  1. 他们已经做了什么

  2. 记录他们在表单中做了什么 raw_function(passed_pa​​rams)

我做了一些错误的假设,这不可能?
任何想法如何Firebug的profiler试图获取函数名称?



我在想什么是制作具有输入/输出的黑盒子:



正常的jquery代码 [BB] 如果你没有使用库,你会写的代码




  • 一个编译器,因为你编译一次,然后会使用结果代码。

  • 我认为它至少可以有教育用途,也可能有其他用途。

  • 人们说这会占用少量代码和输出巨大的质量;

  • 人们说我会在页面渲染中添加一个额外的,不必要的步骤,只有最终得到的代码才会被使用(可能只用于学习),是不正确的。

  • 人们说javascript函数和jquery函数之间没有一对一的关系,并且暗示这样的转换器会太复杂,可能不值得的努力。


class =h2_lin>解决方案

我想你的意思是:如果你写

  var myId = $(#myId)

会将其转换为

  varmyId = document.getElementById(myId)

我认为它的可能,但问题是,jQuery函数返回jQuery对象,所以在上面的例子中,第一个myId将是一个jQuery对象&第二个将是一个节点对象(我想),这将影响需要在编译后的代码中使用它的其他函数。特别是如果他们链接



其次,你必须确保转换实际上具有性能的好处。
但是如果你知道这一切,你可以相应地计划你的代码,我认为这是可能的


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.

This is how I vision a block of jQuery code execution:

  1. a jQuery function is called and parameters are passed to it
  2. the function calls a raw javascript function and passes the parameters it received to it
  3. the newly called function performs the intended action

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.

If we subscribe to that model, then we might come up with methods to make the jQuery functions perform double-duty:

  1. What they already do
  2. Logging what they did in form of raw_function(passed_params)

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?

Edit

What I was thinking was making a black box with input / output as:

normal jquery code[BB]code you'd write if you used no library

  • 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.

Thank you all!

解决方案

I think what you mean is: if you write

var myId = $("#myId")

it will be converted to

var myId = document.getElementById("myId")

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 *编译器可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆