你的位置:首页 > Java教程

[Java教程]javascript AOP实现


参考:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html

    function Person(){      this.say = function(name,lang){        console.log("我的名字叫做"+name+",专注于"+lang+"...");      }    }        (function() {      //AOP(面向切面编程)的javaScript实现      jsAOP = {        /**         * target 切入的目标对象         * method 切入的目标函数(相对于target)         * advice 切入点执行函数         * **/        before:function(target,method,advice){          //备份原函数          var original = target[method];          target[method] = function(){            (advice)();            original.apply(target, arguments);          };          return target        },        after:function(target,method,advice){          var original = target[method];          target[method] = function(){            original.apply(target, arguments);            (advice)();          };          return target        },        around:function(target,method,advice){          var original = target[method];          target[method] = function(){            (advice)();            original.apply(target, arguments);            (advice)();          };          return target        }      };    }());        window.onload = function(){      var p = new Person ();      p = jsAOP.before(p,"say",function() {        console.log("请你介绍一下自己!");      });      p = jsAOP.after(p,"say",function() {        console.log("嗯,不错,明天来上班吧!");      });      p = jsAOP.around(p,"say",(function() {        var index = -1;        return function() {          index++;          if(index == 0) {            console.log("介绍开始!");          }else{            console.log("介绍结束!");            index = -1;          }        };      }()));      p.say("赖祥燃","javascript");    };

参考:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html