Skip to content

Instantly share code, notes, and snippets.

Created October 28, 2010 15:08
Show Gist options
  • Save anonymous/651536 to your computer and use it in GitHub Desktop.
Save anonymous/651536 to your computer and use it in GitHub Desktop.
Function.prototype.beforeStatic = function(methodName, newFunc) {
var oldFunc = this.prototype[methodName];
this.prototype[methodName] = function() {
newFunc.apply(this, arguments);
return oldFunc.apply(this, arguments);
};
};
Function.prototype.afterStatic = function(methodName, newFunc) {
var oldFunc = this.prototype[methodName];
this.prototype[methodName] = function() {
var oldFuncResult = oldFunc.apply(this, arguments);
var newArgsArr = Array.prototype.slice.call(arguments);
newArgsArr.push(oldFuncResult);
newFunc.apply(this, newArgsArr);
return oldFuncResult;
};
};
Function.prototype.beforeInstance = function(instance, methodName, newFunc) {
var oldFunc = instance[methodName];
instance[methodName] = function() {
newFunc.apply(instance, arguments);
return oldFunc.apply(instance, arguments);
};
};
Function.prototype.afterInstance = function(instance, methodName, newFunc) {
var oldFunc = instance[methodName];
instance[methodName] = function() {
var oldFuncResult = oldFunc.apply(instance, arguments);
var newArgsArr = Array.prototype.slice.call(arguments);
newArgsArr.push(oldFuncResult);
newFunc.apply(instance, newArgsArr);
return oldFuncResult;
};
};
Function.prototype.beforeConstruction = function(newFunc) {
var oldObj = this;
var newObj = function() {
newFunc.apply(this, arguments);
oldObj.apply(this, arguments);
}
newObj.prototype = oldObj.prototype;
return newObj;
};
Function.prototype.afterConstruction = function(newFunc) {
var oldObj = this;
var newObj = function() {
oldObj.apply(this, arguments);
newFunc.apply(this, arguments);
}
newObj.prototype = oldObj.prototype;
return newObj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment