[英]Functional jQuery: Custom Events or Functions?
Being new to functional programming, I was working on my application's javascript (using jQuery exclusively), and wondering how jQuery's event system relates to it's functional programming paradigm. 作为函数式编程的新手,我正在研究应用程序的javascript(仅使用jQuery),并想知道jQuery的事件系统如何与其函数式编程范例相关联。
I have a function setupAccountHeader that could easily be either a document-bound event like 我有一个setupAccountHeader函数,可以很容易地是一个文档绑定的事件,例如
$(document).bind('setupAccountHeader', function() {
blah, blah, blah;
});
OR 要么
var setupAccountHeader = function() {
blah, blah, blah;
};
Then, either triggered or called when needed. 然后,在需要时触发或调用。
So I have two questions: 所以我有两个问题:
1) Which style is preferred for functional jQuery or is this purely a matter of taste/personal-style? 1)函数式jQuery首选哪种样式,或者这纯粹是个人风格/风格的问题? What about implications for big-picture architecture?
大图架构的含义如何?
2) Are there any good resources for functionally-styled jQuery? 2)是否有任何功能强大的jQuery资源? I feel like this is John Resig's intent, but I can't find much in the way of info or guides on what this means in implementation.
我觉得这是John Resig的意图,但是我在信息或指南中找不到太多的实现方式。
The nice thing about the second style is that it will show in the debugger call stack with its name, as opposed to "anonymous", which I find a bit more helpful. 关于第二种样式的好处是,它将以其名称显示在调试器调用堆栈中,而不是“ anonymous”,我发现它会有所帮助。
You could achieve the above along with jQuery's added event mechanisms (as Elzo said) with the following: 您可以通过以下方法以及jQuery的添加事件机制(如Elzo所述)实现上述目标:
$(document).bind('setupAccountHeader', setupAccountHeader);
Update 更新
If you to bind events and use the benefits of a normalized event system you'll use the first version for regular (blur, focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, error) or your custom events. 如果您绑定事件并利用标准化事件系统的优势,则可以将第一个版本用作常规版本(模糊,聚焦,加载,调整大小,滚动,卸载,卸载前,单击,dblclick,mousedown,mouseup,mousemove,mouseover, mouseout,mouseenter,mouseleave,更改,选择,提交,keydown,keypress,keyup,error)或您的自定义事件。
They are actually not similar. 它们实际上并不相似。 In order to be similar in the second one you'll have to attach to document some events.
为了与第二篇类似,您必须附加文档以记录一些事件。 Try to cancel them to get the idea.
尝试取消他们的想法。
// first version
$(document).unbind('setupAccountHeader'); // will cancel the binding of some action
// second version
setupAccountHeader = null; // will just cancel a variable
The other version is just a closure that you can use everywhere in JavaScript, and is used in this case as well. 另一个版本只是一个闭包,您可以在JavaScript中的任何地方使用它,在这种情况下也可以使用。 It doesn't have any specific meaning without a context.
没有上下文,它没有任何特定的含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.