Skip to content

Instantly share code, notes, and snippets.

@philikon
Created May 2, 2010 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philikon/386854 to your computer and use it in GitHub Desktop.
Save philikon/386854 to your computer and use it in GitHub Desktop.
// Consider a function:
function multiply(a, b) {
return a*b;
}
print(multiply(Math.PI, Math.exp(1)))
// We want to replace it with an "enhanced" version that does some
// stuff before invoking the original, e.g. generate debugging output
function newMultiply(a, b) {
a = Math.round(a);
b = Math.round(b);
print("DEBUG: round product = " + a*b);
// Invoke original exactly like we were invoked
return oldMultiply.apply(this, arguments);
}
// "Patch" in the new function
var oldMultiply = multiply;
multiply = newMultiply;
// Now the same line from above:
print(multiply(Math.PI, Math.exp(1)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment