Created
August 7, 2013 09:50
-
-
Save pablojim/6172703 to your computer and use it in GitHub Desktop.
wrap method
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
/** | |
* Wrap a method with extended functionality, preserving the original function | |
* @param {Object} obj The context object that the method belongs to | |
* @param {String} method The name of the method to extend | |
* @param {Function} func A wrapper function callback. This function is called with the same arguments | |
* as the original function, except that the original function is unshifted and passed as the first | |
* argument. | |
*/ | |
function wrap(obj, method, func) { | |
var proceed = obj[method]; | |
obj[method] = function () { | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(proceed); | |
return func.apply(this, args); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment