Created
July 24, 2012 23:06
-
-
Save jwalsh/3173251 to your computer and use it in GitHub Desktop.
This file contains 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
var log = function(msg) { | |
window.console && | |
typeof console.log === 'function' && | |
console.log(msg); | |
}; | |
// http://tlrobinson.net/blog/2008/10/wrapping-javascript-callbacks/ | |
function callbackWrap(object, property, argumentIndex, wrapperFactory, extra) { | |
var original = object[property]; | |
object[property] = function() { | |
arguments[argumentIndex] = wrapperFactory(arguments[argumentIndex], extra); | |
return original.apply(this, arguments); | |
}; | |
return original; | |
} | |
function logWrapper(func, msg) { | |
return function() { | |
log(msg); | |
func.apply(this. arguments); | |
}; | |
} | |
callbackWrap(window, "setTimeout", 0, logWrapper, "wrapped a window.setTimeout!"); | |
callbackWrap(window, "setInterval", 0, logWrapper, "wrapped a window.setInterval!"); | |
callbackWrap(window, "addEventListener", 1, logWrapper, "wrapped a window.addEventListener!"); | |
callbackWrap(Element.prototype, "addEventListener", 1, logWrapper, "wrapped a Element.addEventListener!"); | |
callbackWrap(Document.prototype, "addEventListener", 1, logWrapper, "wrapped a Document.addEventListener!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment