第一种:
function hero(e){
console.log(e);
this.kill = (e)=>{
console.log(e);
return this;
}
this.remove = (e)=>{
console.log(e);
return this;
}
return this;
}
hero(‘kk’).kill(1).remove(2)
kk
1
2
第二种:
function hero(e){
console.log(e);
}
hero.prototype.kill = function(e){
console.log(e);
return this;
}
hero.prototype.recover = function(e){
console.log(e);
return this;
}
new hero(‘kk’).kill(1).recover(2);
kk
1
2
注意:链式调用,一定要注意调用者。
如果没有return一个对象的方法,要new一个对象,调用它的原型