javascript 中的方法注入

Posted asdfq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 中的方法注入相关的知识,希望对你有一定的参考价值。

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);


以上是关于javascript 中的方法注入的主要内容,如果未能解决你的问题,请参考以下文章