-
-
Save vasanthk/5edd3a1f5f1231221fa4 to your computer and use it in GitHub Desktop.
JS monkey patching
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
// 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