Copy Code code as follows:
function Actsasdecorator (object) {
Object.setupdecoratorfor = function (method) {
if (! (' Original_ ' + method in object) {
object[' Original_ ' + method] = Object[method];
object[' before_ ' + method] = [];
object[' After_ ' + method] = [];
Object[method] = function () {
var i;
var b = this[' before_ ' + method];
var a = this[' After_ ' + method];
var rv;
for (i = 0; i < b.length; i++) {
B[i].call (this, arguments);
}
RV = this[' original_ ' + method].apply (this, arguments);
for (i = 0; i < a.length; i++) {
A[i].call (this, arguments);
}
return RV;
}
}
};
Object.before = function (method, f) {
Object.setupdecoratorfor (method);
object[' before_ ' + method].unshift (f);
};
Object.after = function (method, f) {
Object.setupdecoratorfor (method);
object[' After_ ' + Method].push (f);
};
}
/**
Invoking
*/
function Test () {
This.say1 = function (s) {
alert (s);
}
This.say2 = function (s) {
alert (s);
}
}
var t = new Test ();
Actsasdecorator (t);
T.before ("Say1", Beforehander);
T.after ("Say2", Afterhander);
Test ();