Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.