简体   繁体   English

功能性jQuery:自定义事件还是功能?

[英]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);
  • The official method of binding an event in jQuery is the first version, this way you have similar methods for binding and unbinding events. 绑定jQuery中事件的官方方法是第一个版本,这样您就可以使用类似的方法来绑定和取消绑定事件。 I think is not that much of a personal preference as it helps code readability, getting support for your code and using other functions related and included in jQuery like preventing default event bubbling. 我认为这并不是个人喜好,因为它有助于代码可读性,对代码的支持以及使用与jQuery相关且包含在内的其他功能,例如防止默认事件冒泡。 The second example of the first question is correct from JavaScript, but this style is not used anywhere in jQuery documentation. 第一个问题的第二个示例在JavaScript中是正确的,但是jQuery文档中没有在任何地方使用此样式。

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. 没有上下文,它没有任何特定的含义。

  • I don't really understand the second question. 我不太明白第二个问题。 Maybe you can detail what you mean. 也许您可以详细说明您的意思。 I'm not a native English speaker. 我不是英语母语人士。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM