链式调用
原理:在构造函数中创建方法时,return this 返回当前调用方法的对象,可以实现链式调用方法。
function Person(name) {
this.name = name
this.sayHi = function() {
console.log(this.name)
return this
}
this.modifyName = function(name) {
this.name = name
return this
}
}
var person = new Person('IU')
person
.sayHi()
.modifyName('UI')
.sayHi()
// IU
// UI
// Person { name: 'UI', sayHi: f, modifyName: f }