Home >Web Front-end >JS Tutorial >The evolution of my javascript function chain_javascript skills

The evolution of my javascript function chain_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:08:101117browse

The most readable version

Copy code The code is as follows:

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
Copy code The code is as follows:

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
Copy code The code is as follows:

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
Copy code The code is as follows:

chain(obj)
(method1,arg1)
(method2,arg2)
(method3,arg3)
...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn