Created
July 5, 2012 07:32
-
-
Save mlsteele/3052034 to your computer and use it in GitHub Desktop.
Intercepts and wraps a method of a js object.
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
// function interceptor | |
var interceptMethod = function(object, methodName, newMethodGen) { | |
previousMethod = object[methodName] | |
object[methodName] = newMethodGen(previousMethod) | |
} | |
// where newMethodGen is of the form | |
foo = {x: function() {console.log('foo.x')}} | |
interceptMethod(foo, 'x', function(f) { | |
return function(n) { | |
console.log('foo.x.pre.'+n) | |
f.call(this) | |
console.log('foo.x.post.'+n) | |
} | |
}) | |
foo.x() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment