Created
September 5, 2011 07:45
-
-
Save sparrovv/1194338 to your computer and use it in GitHub Desktop.
Simple js asspects
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 Asspect = (function(){ | |
var add = function(object, functionName, opts){ | |
object = object || window; | |
var originalFunction = object[functionName]; | |
// check if function exists | |
object[functionName] = function(){ | |
if (opts.before){ | |
opts.before.apply(object, arguments); | |
} | |
var originalFunctionCall = originalFunction.apply(object, arguments); | |
if (opts.after){ | |
opts.after.apply(object, arguments); | |
} | |
return originalFunctionCall; | |
}; | |
}; | |
return { | |
add: add | |
}; | |
}()); | |
var foo = function(a){ console.log(a); }; | |
var before = function(){ console.log("before"); }; | |
var after = function(){ console.log("after"); }; | |
Asspect.add(null, 'foo', {before:before, after:after}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment