Skip to content

Instantly share code, notes, and snippets.

@sairion
Forked from naholyr/monkey-patch.js
Created December 26, 2013 12:05
Show Gist options
  • Save sairion/8132928 to your computer and use it in GitHub Desktop.
Save sairion/8132928 to your computer and use it in GitHub Desktop.
// 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