The JSChain itself is very small. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works.

functionJSChain(obj)

{

var self =this;

this.____items =[];//this remembers the callback functions list

this.____finally =null;

this.____next=function(jump)//execute next function

{

if(!jump)

{

var func = self.____items.shift();

if(func) func.call();

}

else self.____items =[];

if(self.____items.length==0&&typeof self.____finally =='function')

{

self.____finally.call(obj);

self.____finally =null;

}

};

this.exec=function()//support for custom function

{

var args =[].slice.call(arguments,1),func = arguments[0];

args.push(self.____next);

self.____items.push(function()

{

func.apply(obj,args);

});

return self;

};

this.end=function(func)//support for final function

{

self.____finally = func;

return self;

};

//copy and wrap the methods of obj

for(var func in obj)

{

if(typeof obj[func]=='function'&& obj.hasOwnProperty(func))

{

(function(func)

{

self[func]=function()

{

var args =[].slice.call(arguments);

args.push(self.____next);//pass next callback to the last argument

self.____items.push(function()//wrap the function and push into callbacks array

{

obj[func].apply(obj,args);

});

return self;//always return JSChain it self like jQuery

}

})(func);// this is the closure tricks

}

}

//start execute the chained functions in next tick

setTimeout(self.____next,0);

returnthis;

}

##Advanced features##

###end method###

An end method can add a function to the end of the chain. You can not define this in the object when new JSChian().

###jump to the end instead of going to next###

When you call next method, if you pass a true value into next() method, that means JUMP TO THE END.