让我试着解释一下。我没有读过那本书,但是 Douglas Crockford 的一篇文章Classical Inheritance in JavaScript有一个重要的句子,与关于 Function.prototype.method 的示例相关:
它返回这个。当我编写一个不需要返回值的方法时,我通常让它返回 this。它允许级联式编程。
实际上我不熟悉这个术语,我认为众所周知的术语是“ Fluent Interface ”或“Method Chaining”,阅读那个wiki页面,有不同语言的示例,所以你会理解它..
PS。@Gianluca Bargelli 以这种方式提供使用 Function.prototype.method 的示例要快一些,所以我不会在我的答案中发布它
ADDON:根据您的示例如何使用它:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
}
Number.method('integer', function () { // you add 'integer' method
return Math[this < 0 ? 'ceil' : 'floor'](this);
})
.method('square', function () { // you add 'square' method with help of chaining
return this * this;
});
console.info( (-10/3).integer().square() ); // <- again chaining in action
你看,integer() 返回 Number 对象,所以你可以调用另一个方法,而不是写:
var a = (-10/3).integer();
console.info( a.square() );
关于我使用它的方式的几句话,大多数时候我更喜欢写“每个方法 - 带有缩进的新行,对我来说这种方式更具可读性:
Function.method('add', add)
.method('sub', sub)
.method('mul', mul)
.method('div', div);
这样我就可以看到我从哪里开始,并且“新行/缩进”告诉我我仍在修改该对象。将其与长线进行比较:
Function.method('add', add).method('sub', sub).method('mul', mul).method('div', div);
或典型方法:
Function.method('add', add);
Function.method('sub', sub);
Function.method('mul', mul);
Function.method('div', div);
ADDON2:通常我在处理实体(例如 Java 代码)时使用这种方法(流利的接口模式):
public class Person {
private String name;
private int age;
..
public String getName() {
return this.name;
}
public Person setName( String newName ) {
this.name = newName;
return this;
}
public int getAge() {
return this.age;
}
public Person setAge( int newAge ) {
this.age = newAge;
return this;
}
..
}
它允许我以Person
简单的方式构造对象:
Person person = new Person().setName("Leo").setAge(20);
有些人让它有点不同,他们在set
/中添加了新的方法get
,并称之为with
:
public class Person {
private String name;
private int age;
..
public String getName() {
return this.name;
}
public void setName( String newName ) {
this.name = newName;
}
public Person withName( String newName ) {
this.setName( newName ); // or this.name = newName; up to you
return this;
}
public int getAge() {
return this.age;
}
public void setAge( int newAge ) {
this.age = newAge;
}
public Person withAge( int newAge ) {
this.setAge( newAge ); // or this.age = newAge; up to you
return this;
}
..
}
现在我之前的示例如下所示:
Person person = new Person().withName("Leo").withAge(20);
这样我们就不会改变 set 方法的含义(我的意思是我们不会增强它,因此它可以像大多数开发人员所期望的那样工作......至少人们不期望该set
方法可以返回任何东西;))。关于这些特殊方法的一件有趣的事情 - 它们可以放松自我记录,但是当您使用它们时它们会提高可读性(例如Person
创建示例,withName
可以很好地说明我们到底在做什么......
阅读更多:
FluentInterface - Martin Fowler 对该模式的描述
Fluent Interfaces in PHP
The Weekly Source Code 14 - Fluent Interface Edition - 对我来说简短且足以看到利弊(以及其他资源的链接)