The chaining mechanism is handy if you want to add more than just one prototype to the function you're extending, for example:
Code:
Function.prototype.method = function(name, func)
{
this.prototype[name] = func;
return this;
};
function azerizor(value)
{
this.value = value;
}
azerizor
.method('toString', function() {return this.value;})
.method('toStringMore', function() {return this.value + ' more';})
.method('bothFuncs', function() {return this.toString() + ' ' + this.toStringMore();});
var me = new azerizor('salam');
alert(me.toString());
alert(me.toStringMore());
alert(me.bothFuncs());
If "return this" was not included then the second and third calls to the "method" function would not have worked.
Just note that there is no semi-colon until right at the end of the chain.
Bookmarks