jQuery 方法链接是流畅编程的一个例子吗?
我对 JavaScript/jQuery 有点陌生,但是当我看到方法链接的示例时,我立即感到熟悉。其他接口(如 LINQ)执行类似的操作,其中一组方法的返回类型与它们所操作的类型相同(TweetSharp 执行的操作非常类似)。这是流畅编程的一个例子吗?我读到的有关 jQuery 的大部分内容都说其他库“借用”了这种方法链接的想法 - 这个想法是否源于 jQuery?
I'm somewhat new to JavaScript/jQuery, but when I saw examples of method chaining it struck me as instantly familiar. Other interfaces like LINQ do something similar where the return type of a set of methods is the same as the type they operate on (TweetSharp does something very similar). Is this an example of fluent programming? Much of what I read about jQuery says that other libraries have "borrowed" this idea of method chaining - did the idea originate with jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 确实严重遵循 构建器模式 / 流畅的界面,但它本身并不是设计模式的先驱。其他库可能效仿 jQuery 并借鉴了它的方法。正是 jQuery 的名声让它看起来像是起源于 jQuery。
jQuery indeed heavily follows the builder pattern / fluent interface, but it is certainly not the pioneer of the design pattern at whole own. The other libraries likely followed after jQuery and borrowed its approach. It's just the fame jQuery has made which make it look like to originate with jQuery.
单独链接并不能形成流畅的界面。术语“流畅的界面”似乎是由马丁·福勒(Martin Fowler)普及的,他是一位多产的人当代软件理论作者(尽管他也没有发明该技术)。
Fowler 澄清说,方法链只是指返回对象引用的方法,而流畅的接口更类似于特定于领域的语言,并且(理想情况下)设计为更像自然语言而不是代码。
Chaining alone does not make a fluent interface. The term "fluent interface" seems to have been popularized by Martin Fowler, one of the more prolific contemporary authors on software theory (though he did not invent the technique either).
Fowler clarifies that method chaining simply refers to methods that return the object reference, while a fluent interface is more akin to a domain specific language, and is (ideally) designed to flow more like natural language than code.
JQuery 当然没有发明方法链;方法链是 JQuery 发明的。 JavaScript 和许多其他语言已经可以实现这一点。
在许多人看来,JQuery 可能使方法链合法化了。许多 Java 程序员仍然坚持认为对象的函数应该只返回其他对象,而不是对象本身。我记得 10 年前,我因提出其他建议而被从 Java 邮件列表中删除:)
JQuery certainly didn't invent of method-chaining; it was already possible in JavaScript and many other languages.
Method-chaining was perhaps legitimized in many people's eyes by JQuery. A lot of Java programmers still insist that an object's functions should only return other objects, never the object itself. As I recall 10 years ago I got modded off a Java mailing list for suggesting otherwise :)