【转】面向切面编程-AOP,挺有用的

原文:http://blog.csdn.net/yangzhihello/article/details/40377399

下面的代码有点问题,

  ins[methodName] = function(){  

              callback.apply(ins,arguments)  

    return method.apply(ins,arguments);  

        }  

这段也要放到上面的if 语句块里面去!!!

 

补充, 通过后面一片aop的文章的理解,我发现,这里的aop的实现方式,会改变原有函数的行为,这种貌似不大好。但是,后面一篇的方式在Function.prototpye上面的方式,好像也不大好。

 

-------------------------------------------------------------------------------------------------------------------

对于java中的aop,早有耳闻,所谓面向切面编程,不过一直都以为只是在后台才会用到。

从做前端开始就没有这方面的需求,这一次对于一些东西做新的想法,发现有些东西还是可以去考虑考虑的。

有一个公用的代码,可能在很多地方都会被用到,那么现在要做的就是,需要这个方法跑起来之前走一些东西,在这个方法跑完之后,还在处理一些东西。

那么问题就来了:

1 直接改之前封装好的东西,Ok,可以没问题,但是这样做了之后,别处用到这个东西的地方就直接崩溃了!

2 那么如果采用直接复制一份代码,修改单个这一处的问题,ok,没有问题,但是这不也就是代表着代码冗余越来越多。

这个时候想起了,在java里面有一个东西,面向切面,添加切点,一切似乎就可以正常的跑起来了。是的,就这么干.....

如下:

由于是工具类,无需实例化,刚直接可以采用{}对象方式:

[javascript] view plain copy
 
  1. aop = window.aop || {};  
  2.   
  3. aop = {  
  4.     doBefore:function(ins,methodName,callback){  
  5.         if(typeof ins[methodName] != "undefined"){  
  6.             var method = ins[methodName];     
  7.         }  
  8.         ins[methodName] = function(){  
  9.             callback.apply(ins,arguments)  
  10.             return method.apply(ins,arguments);  
  11.         }  
  12.     },  
  13.     doAfter:function(ins,methodName,callback){  
  14.         if(typeof ins[methodName] != "undefined"){  
  15.             var method = ins[methodName];  
  16.         }  
  17.         ins[methodName] = function(){  
  18.             var ret = method.apply(ins,arguments),  
  19.                 callret = callback.apply(ins,arguments);  
  20.             return typeof callret == "undefined" ? ret : callret;  
  21.         }  
  22.     }  
  23. }  
 

html 中测试如下:

[html] view plain copy
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">  
  3. <head>  
  4.     <script type="text/javascript" src="aop.js"></script>  
  5.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">  
  6.     <title>aop javascript</title>  
  7. </head>  
  8. <body>  
  9.     <script type="text/javascript">  
  10.         //json对象  
  11.         var obj = {  
  12.             run:function(){  
  13.                 console.log("run");  
  14.             },  
  15.             init:function(name){  
  16.                 console.log("arguments : " + name);  
  17.                 return "info";  
  18.             }  
  19.         };  
  20.   
  21.   
  22.         aop.before(obj,"run",function(){  
  23.             console.log("run before");  
  24.         });  
  25.         aop.after(obj,"run",function(){  
  26.             console.log("run after");  
  27.         });  
  28.         console.log("<!-----json对象--------->");  
  29.         obj.run();  
  30.   
  31.         var F = function(){};  
  32.         F.prototype = {  
  33.             run:function(){  
  34.                 console.log("prototype run");  
  35.             }  
  36.         };  
  37.         //创建对象  
  38.         console.log("<!-----创建对象--------->");  
  39.         var f = new F();  
  40.   
  41.         aop.before(f,"run",function(){  
  42.             console.log("prototype run before");  
  43.         });  
  44.         aop.after(f,"run",function(){  
  45.             console.log("prototype run after");  
  46.         });  
  47.         f.run();  
  48.   
  49.         //参数问题  
  50.         console.log("<!-----参数问题--------->");  
  51.         aop.before(obj,"init",function(name){  
  52.             console.log("arguments before :" + name);  
  53.         });  
  54.         aop.after(obj,"init",function(name){  
  55.             console.log("arguments after :" + name);  
  56.             return "next";  
  57.         });  
  58.   
  59.         console.log(obj.init("user"));  
  60.   
  61.     </script>  
  62. </body>  
  63. </html>  
 


这里多种情况,包括参数的传递问题,皆有考虑!

偶有所得,以记录之,谨防忘记!

原文地址:https://www.cnblogs.com/oxspirt/p/8410550.html