oop(面向对象编程),aop(面向方面编程)。
oop就是把多个对象的相同功能单独分离出去,封装成组件或者函数。
oop举例:
前端的组件和函数;axios封装,权限认证、日志、事务处理等。
oop主要分两类:
1,一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;(前端我想到了装饰器(Decorator))
2,是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码
在 JavaScript 中实现 AOP,一般都是指把一个函数“动态织入”到另外一个函数之中
扩展 Function.prototype示例:
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;
}
};
var func = function(){
console.log( 2 );
};
func = func.before(function(){
console.log( 1 );
}).after(function(){
console.log( 3 );
});
func();