Friday 21 June 2013

Adding Codes to an Existing JavaScript Function

Below is a script to add codes (prepend or append) to an existing JavaScript function:
myFunction = (function () {
 // check whether myFunction already exists
 if (typeof myFunction == 'function') {
  var existingFunction = myFunction;
 }
 return function () {
  // some codes before existing function
  // . . .
 
  if (typeof existingFunction != 'undefined') {
   existingFunction.apply(this, arguments); 
   // by using 'apply' we can pass one or more arguments that is initially passed to the original function
  }
  
  // some codes after existing function
  // . . .
 };
} ());  

No comments: