Home > Web Front-end > JS Tutorial > body text

The evolution of my javascript function chain_javascript skills

WBOY
Release: 2016-05-16 18:08:10
Original
1108 people have browsed it

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)
...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template