The most readable version
function chain(obj){
function fun(){
if (arguments.length == 0){
return fun.obj;
}
var methodName = arguments[0], methodArgs = [].slice.call (arguments,1);
fun.obj[methodName].apply(fun.obj,methodArgs);
return fun;
}
fun.obj = obj;
return fun;
}
Easy to read version
function chain(obj){
return function(){
var Self = arguments.callee; Self.obj = obj;
if(arguments.length==0){
return Self.obj;
}
var methodName = arguments[0], methodArgs = [].slice.call(arguments,1);
Self.obj[methodName].apply(Self.obj, methodArgs);
return Self;
}
}
Lite version
function chain(obj){
return function(){
var Self = arguments.callee; Self.obj = obj;
if (arguments.length==0){
return Self.obj;
}
Self.obj[arguments[0]].apply(Self.obj,[].slice.call(arguments,1 ));
return Self;
}
}
Call
chain(obj)
(method1,arg1)
(method2,arg2)
(method3,arg3)
...