Skip to content

Instantly share code, notes, and snippets.

@vasanthk
Forked from naholyr/monkey-patch.js
Created December 1, 2015 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasanthk/5edd3a1f5f1231221fa4 to your computer and use it in GitHub Desktop.
Save vasanthk/5edd3a1f5f1231221fa4 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