鏈?zhǔn)秸{(diào)用在JavaScript編程中還是很常見(jiàn)的,能夠減少代碼量,并且讓邏輯清晰,更易讀。
第一種:返回this
class Test1{
then(){
console.log(6666);
return this;
}
}
var a= new Test1();
a.then().then().then()
這里很好理解,就是返回了本身,那么就能繼續(xù)調(diào)用
第二種: 返回新的實(shí)例
class Test2{
then(){
console.log(77777);
return new Test2();
}
}
var b= new Test2();
b.then().then().then()
在這里相當(dāng)于返回了一個(gè)新的Test2的實(shí)例,就和Promise里面實(shí)現(xiàn).then鏈?zhǔn)秸{(diào)用的時(shí)候,返回的是一個(gè)新的Promise是一個(gè)意思