Skip to content

Instantly share code, notes, and snippets.

@mlsteele
Created July 5, 2012 07:32
Show Gist options
  • Save mlsteele/3052034 to your computer and use it in GitHub Desktop.
Save mlsteele/3052034 to your computer and use it in GitHub Desktop.
Intercepts and wraps a method of a js object.
// 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