AOP 面向切面编程 All In One
Aspect-Oriented Programming / 面向切面编程
demos
// @decorator 修饰器/装饰器
Function.prototype.before = function( beforefn ){
var __self = this; // 保存原函数的引用
return function(){ // 返回包含了原函数和新函数的"代理"函数
beforefn.apply( this, arguments ); // 执行新函数,修正this
return __self.apply( this, arguments ); // 执行原函数
}
}
Function.prototype.after = function( afterfn ){
var __self = this;
return function(){
var ret = __self.apply( this, arguments );
afterfn.apply( this, arguments );
return ret;
}
}
Function.prototype.around = function(beforefun,afterfun){
let __self = this
return function(){
return __self.before(beforefun).after(afterfun).apply(this,arguments)
}
}
var func = function(){
console.log( 2 );
};
func = func.before(function(){
console.log( 1 );
}).after(function(){
console.log( 3 );
});
func();