Copy codeThe Code is as follows: function actsAsDecorator (object ){
Object. setupDecoratorFor = function (method ){
If (! ('Ininal _ '+ method in object )){
Object ['al inal _ '+ 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 ['ininal _ '+ 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 ();