Created
September 19, 2009 01:09
-
-
Save usergenic/189370 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
// We can't use super because it's apparently reserved in some JS implementations. | |
var superf = function(){}; | |
function overload(object, methodName, callback){ | |
var superMethod = object[methodName]; | |
object[methodName] = function(){ | |
var _superf = superf; | |
var me = this; | |
superf = function(){ | |
return superMethod.apply(me, arguments); | |
} | |
var result = callback.apply(this, arguments); | |
superf = _superf; | |
return result; | |
}; | |
} | |
a = {"m":function(a,b){return a+b;}}; | |
print(a.m(1,2)); // 3 | |
overload(a, "m", function(a,b){ | |
return superf(a*10, b); | |
}); | |
print(a.m(1,2)); // 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment