Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created December 21, 2012 11:52
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save naholyr/4352358 to your computer and use it in GitHub Desktop.
Save naholyr/4352358 to your computer and use it in GitHub Desktop.
JS monkey patching
// Original method
var object = {
method: function (x, y) {
return x+y;
}
}
// Add operations before or after!
object.method = (function (original) {
return function (x, y) {
// before
// we could here modify "arguments" to alter original input
console.log(x, '+', y, '?');
// execute
var result = original.apply(this, arguments);
// after
// we could here work on result to alter original output
console.log('=', result);
// aaaand the result
return result;
}
})(object.method);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment