jQuery 方法链接是流畅编程的一个例子吗?

发布于 2024-09-12 20:36:59 字数 192 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爺獨霸怡葒院 2024-09-19 20:37:00

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.

眼泪都笑了 2024-09-19 20:37:00

单独链接并不能形成流畅的界面。术语“流畅的界面”似乎是由马丁·福勒(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.

策马西风 2024-09-19 20:37:00

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文