我的函数链之演变
最易读版
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; }
易读版
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; } }
精简版
/** * chain 精简版 * @param {Object} obj */ function chain(obj) { var slice = [].slice return function() { var Self = arguments.callee var Self.obj = obj if (arguments.length === 0) { return Self.obj } Self.obj[arguments[0]].apply(Self.obj, slice.call(arguments, 1)) return Self } }
调用
chain(obj) (method1,arg1) (method2,arg2) (method3,arg3) ...