原理很简单每次执行完函数返回this
直接上代码
function test(){
this.name;
this.age;
}
test.prototype.testChild=function(){
this.name="demo"
return this
}
test.prototype.testChild2=function(){
this.age=20
return this.name+":"+this.age
}
var a=new test()
var b=a.testChild().testChild2()
console.log(b)//demo:20
本文介绍了一种在JavaScript中实现链式调用的方法,通过构造函数和原型链,使得多个方法可以连续调用,提高了代码的可读性和效率。
485

被折叠的 条评论
为什么被折叠?



