在类的方法中使用return this,没什么说的,就是实现链式调用。
function AnimalSounds(){
}
AnimalSounds.prototype.cow = function(){
console.log("moo");
return this;
}
AnimalSounds.prototype.dog = function(){
console.log("woof");
return this;
}
var sounds = new AnimalSounds();
当没有使用return this时:
sounds.cow();
sounds.dog();
当使用return this时
sounds.cow().dog();
浙公网安备 33010602011771号