通过返回对自身的引用来链接对象中的方法的模式是否有名称?
在大多数现代面向对象语言中,将方法链接在一起很常见,而且恕我直言,实践很优雅。例如,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法链是构建流畅界面
Method Chaining is the core concept behind building a Fluent Interface
这称为流畅的界面。
资源
This is called a Fluent Interface.
Resources
它通常称为 "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.
Douglas Crockford 将其称为“级联式”编程。
Douglas Crockford calls it a "cascade-style" of programming.