js 中的方法注入(aop)

js 中的方法注入

java中很多框架支持 apo 的注入, js中也可以类似的进行实现
主要是通过扩展js中方法的老祖 Function 对象来进行实现.

Function.prototype.after = function(foo) {
    const thiz = this;
    return function(...args) {
         thiz.apply(thiz, args);
         foo.apply(thiz, args);
 
    }
}
//test
function test(param) {
    console.log("do");
}
function doAfter(param) {
    console.log("doAfter");
}
const doWithAfter = test.after(doAfter);

before 也是类似的. 另外还可以通过 Proxy 进行实现.

原文地址:https://www.cnblogs.com/asdfq/p/10435028.html