//定义的function/类ClassB
function ClassB(){
this.prop1 = null;
this.prop2 = null;
this.prop3 = null;
}
ClassB.prototype = {
method1 : function(p1){
this.prop1 = p1;
},
method2 : function(p2){
this.prop2 = p2;
},
method3 : function(p3){
this.prop3 = p3;
}
}
注意ClassB的method1,method2,method3中不再返回this了。
链式调用如下:
代码如下:
var obj = new ClassB();
chain(obj)('method1',4)('method2',5)('method3',6); // obj -> prop1=4,prop2=5,prop3=6
第一种方式3次调用后返回了对象自身,这里使用一个空"()"取回对象
代码如下:
// result -> prop1=4,prop2=5,prop3=6
var result = chain(obj)('method1',4)('method2',5)('method3',6)();
这种方式写类时方法体中无须返回this,且可以对任何对象进行链式调用。
从写法上总结下两种的调用方式:
代码如下:
obj
.method1(arg1)
.method2(arg2)
.method3(arg3)
...
chain(obj)
(method1,arg1)
(method2,arg2)
(method3,arg3)
...
最后,感谢沐海,我是从wee库中获取以上灵感的。
/201101/yuanma/chain.rar
Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务