Monday, March 1, 2010

chainable generic javascript objects

yeah...is a mouth full but it is insanely good stuff.



function GenericObj(){
var ChainableObject = function(){ return new ChainableObject.fn.init();};
ChainableObject.fn = ChainableObject.protoype = {};
ChainableObject.fn.init = function(){};
ChainableObject.fn.init.prototype = ChainableObject.fn;
return ChainableObject;
}



My buddy and co-worker Mahdi (don't worry, the site has been under construction forever...i just needed somewhere to link) are currently working on a jQuery toolkit that will be module based. jQuery ToolKit (yeah, that too is under construction). This little nugget is in the toolkit and more gold like this you can expect to find.

1 comment:

Mahdi Pedramrazi said...

nice and amazing post scott .
sample :
var obj = new GenericObject();
obj.fn.myfunc1 = function(){
// Calling myfunc2 two times
this.myfunc2().myfunc2();
return this;

};
obj.fn.myfunc2 = function(){
// do something
return this;
};

tip : you should have return this in your functions to be chainable