Last active
December 30, 2015 06:49
-
-
Save jondavidjohn/7791785 to your computer and use it in GitHub Desktop.
How to extend JavaScript functions
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
Array.prototype.join = (function(_super) { | |
// return our new `join()` function | |
return function() { | |
console.log("Hey, you called join!"); | |
return _super.apply(this, arguments); | |
}; | |
// Pass control back to the original join() | |
// by using .apply on `_super` | |
})(Array.prototype.join); | |
// | |
// Pass the original function into our | |
// immediately invoked function as `_super` | |
// which remains available to our new | |
// function thanks to JavaScript Closures. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment