JavaScript实现类似java编程中AOP编程方法,动态顺序执行函数

参考:https://www.cnblogs.com/yonglin/p/8059183.html 

1> 通过以上博主研究发现js中可以这样去实现代码,个人感觉这样写好处很大,首先封装组件,公共的请求等有很大的优势,可以去掉很多冗余的代码

   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();
原文地址:https://www.cnblogs.com/han-guang-xue/p/11754126.html