话不多说,直接贴代码
核心代码return this
这里的this
指代的是当前的checkObject
对象,所以每次调用该对象里面的方法的时候,都会返回当前对象
var checkObject = {
checkName: function () {
console.log(this);
return this
},
checkEmail: function () {
console.log(this);
return this
},
checkPwd: function () {
console.log(this);
return this
}
}
checkObject.checkName().checkEmail().checkPwd()
输出结果如下:

该方法实现起来很简单,但在实际开发中非常实用
网友评论