演变的javascript函数链
最易读版
01 function chain(obj){
02 function fun(){
03 if (arguments.length == 0){
04 return fun.obj;
05 }
06 var methodName = arguments[0], methodArgs = [].slice.call(arguments,1);
07 fun.obj[methodName].apply(fun.obj,methodArgs);
08 return fun;
09 }
10 fun.obj = obj;
11 return fun;
12 }
易读版
01 function chain(obj){
02 return function(){
03 var Self = arguments.callee; Self.obj = obj;
04 if(arguments.length==0){
05 return Self.obj;
06 }
07 var methodName = arguments[0], methodArgs = [].slice.call(arguments,1);
08 Self.obj[methodName].apply(Self.obj,methodArgs);
09 return Self;
10 }
11 }
精简版
01 function chain(obj){
02 return function(){
03 var Self = arguments.callee; Self.obj = obj;
04 if(arguments.length==0){
05 return Self.obj;
06 }
07 Self.obj[arguments[0]].apply(Self.obj,[].slice.call(arguments,1));
08 return Self;
09 }
10 }
调用
1 chain(obj)
2 (method1,arg1)
3 (method2,arg2)
4 (method3,arg3)
5 ...