2024年05月25日 建站教程
什么是JavaScript语法中的链式调用?链式调用是指在一个对象上连续调用多个方法。要实现链调用,需要在每个方法的末尾返回对当前对象的引用。
具体语法示例如下:
var obj = {
add: function(x) {
this.num += x;
return this;
},
sub: function(x) {
this.num -= x;
return this;
},
num: 0
};
obj.add(5).sub(3).add(10);
console.log(obj.num); // 输出 12
本文链接:http://so.lmcjl.com/news/5309/