通过返回对自身的引用来链接对象中的方法的模式是否有名称?

发布于 2024-09-24 15:17:54 字数 182 浏览 1 评论 0原文

在大多数现代面向对象语言中,将方法链接在一起很常见,而且恕我直言,实践很优雅。例如,在 jquery 中,您经常会看到如下代码:

$('div').addClass('container').css('color', 'white').length

编写对象以允许此操作是否有名称?

In most modern OO languages chaining methods together is common, and IMHO elegant, practice. In jquery, for example, you often see code like:

$('div').addClass('container').css('color', 'white').length

Does writing your objects to allow this have a name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

遗心遗梦遗幸福 2024-10-01 15:17:54

Method Chaining is the core concept behind building a Fluent Interface

西瑶 2024-10-01 15:17:54

这称为流畅的界面。

资源

This is called a Fluent Interface.

Resources

无风消散 2024-10-01 15:17:54

它通常称为 "fluent",其中每个方法都会返回对接下来要处理的对象的引用,允许您将调用链接在一起。

在 jQuery 教程等中,您会听到它被称为“链接”...这只是社区中更广泛使用的另一种描述,它允许而不是它是什么,它仍然是一个流畅的界面。

It's typically called "fluent" where each method returns a reference to the object you want to deal with next, allowing you to chain calls together.

In jQuery tutorials and such you'll hear it called "chaining"...it's just another description more heavily used in the community of what it allows not what it is, it's still a fluent interface.

揽清风入怀 2024-10-01 15:17:54

Douglas Crockford 将其称为“级联式”编程。

Douglas Crockford calls it a "cascade-style" of programming.

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