Sunday, April 15, 2012

Extending a Javascript function without affecting the original function defenition.

There are situations like need to extend ( append / prepend your own code within a function body) predefined function.
Here is a useful way to do this without affecting the original function defenition.

example;
function originalFunction(arg1,arg2,arg3)
{

alert(arg1 + arg2 + arg3);

}

var temp=originalFunction;
originalFunction=function(){
//precode.
alert("this is precode.");

//calling originalFunction
temp.apply({},arguments);

//postcode
alert("this is postcode.");

}
You can call this as
originalFunction(1,2,3);


hope this helps...

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails