javascript的aop实现

<script type="text/javascript">
   var aop = (function(){
         var targetFnName;
         var context = window;
         var options;
         var checkConfig = function (){
               /*
                *判断传的参数是否为对象
                */
             if(options && typeof options === "object"){  
                   if(options.context) {
                       context = options.context;
                   }

                   var isNotError = true;
                    
                   if(typeof context === "function"){ //为一个类中的函数做aop
                        if(typeof context.prototype[options.target] === "function") {
                            targetFnName = options.target;
                        }else{
                             isNotError = false;
                        }     
                   }else if(typeof context === "object"){ //为一个对象中的函数做aop
                        if(typeof context[options.target] === "function") {
                            targetFnName = options.target;
                        }else{
                             isNotError = false;
                        }
                   }

                   return isNotError;
                    

              }   
         }
         var createAop = function (beforeFn,afterFn,aroundFn){
                  var oFn;
                  var realFunc =  function (){
                  if(aroundFn){
                     aroundFn.apply(this,arguments);
                  }
                  if(beforeFn){
                      var next = beforeFn.apply(this,arguments);
                  }
                  if(next){
                      oFn.apply(this,arguments);
                      if(afterFn){
                          afterFn.apply(this,arguments);
                      }
                      if(aroundFn){
                         aroundFn.apply(this,arguments);
                      }
                  }                   
                }
                  if(typeof context === "function"){ //为一个类中的函数做aop
                    oFn = context.prototype[targetFnName];   
                    context.prototype[targetFnName] = realFunc;
                }else if(typeof context === "object"){ //为一个对象中的函数做aop
                    oFn = context[targetFnName];
                    context[targetFnName] = realFunc;
                }
         };

         return function (opt){
               options = opt;
             if(options.target && checkConfig()) { //判断织入点是否为空并校验配置是否有问题
                   var beforeFn = "",afterFn = "",aroundFn = "";
                   if(typeof options.beforeFn === "function"){
                       beforeFn = options.beforeFn;
                   }
                   if(typeof options.afterFn === "function"){
                       afterFn = options.afterFn;
                   }
                   if(typeof options.aroundFn === "function"){
                       aroundFn = options.aroundFn;
                   }
                   createAop(beforeFn,afterFn,aroundFn);
             }
         }
   })();

var aaa = function (){
  alert("aaa");
}

aop({
   context:this,
   target:"aaa",
   beforeFn:function(){
       alert("aop first!");
       return true;
   },
   afterFn:function(){
       alert("aop last!");
   }
});

aaa();
/*输出:
aop first!
aaa
aop last!*/

//以下是dog类的写法
var dog = function (){
   this.age = 0;
   this.name = "Terrance";
};
dog.prototype.setAge = function (age) {
   this.age = age;
};
dog.prototype.getAge = function () {
   alert(this.age);
};
dog.prototype.getName = function () {
   alert(this.name);
};

aop({
   context:dog,
   target:"getAge",
   beforeFn:function(){
     alert("aop first2!");
     return true;
   },
   afterFn:function(){
       alert("aop last2!");
   }
});

var newDog = new dog();
newDog.setAge(40);
newDog.getAge();

/*输出:
aop first!
40
aop last!*/
aaa();
</script>


你可能感兴趣的:(javascript的aop实现)