return this;

Posted on 2017-08-29 15:15  李小白QAQ  阅读(116)  评论(0)    收藏  举报

在类的方法中使用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();