Created
May 2, 2010 02:58
-
-
Save philikon/386854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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