zoukankan      html  css  js  c++  java
  • js 中实现aop

    http://fredrik.appelberg.me/2010/05/07/aop-js/

    Aop = {
      // Apply around advice to all matching functions in the given namespaces
      around: function(pointcut, advice, namespaces) {
        // if no namespaces are supplied, use a trick to determine the global ns
        if (namespaces == undefined || namespaces.length == 0) 
          namespaces = [ (function(){return this;}).call() ];
        // loop over all namespaces 
        for(var i in namespaces) {
          var ns = namespaces[i];
          for(var member in ns) {
            if(typeof ns[member] == 'function' && member.match(pointcut)) {
              (function(fn, fnName, ns) {
                 // replace the member fn slot with a wrapper which calls
                 // the 'advice' Function
                 ns[fnName] = function() {
                   return advice.call(ns, { fn: fn, 
                                              fnName: fnName, 
                                              arguments: arguments });
                 };
               })(ns[member], member, ns);
            }
          }
        }
      },
     
      next: function(f) {
        return f.fn.apply(this, f.arguments);
      }
    };
    

      

    example:

    function hello(){
      console.log('hello');
    }
    
    Aop.around('hello',function(fun){
      console.log('before');
      Aop.next(fun);
      console.log('after');
    });
    

      

  • 相关阅读:
    Spring第一次测试错题解析
    正则回顾
    Spring经典---AOP
    动态代理
    MyBatis第一次测试卷---错题分析
    JS中对数组元素进行增删改移
    限制条件补全代码系列题
    字符串去空格
    数组去重
    数组排序
  • 原文地址:https://www.cnblogs.com/zyip/p/5113035.html
Copyright © 2011-2022 走看看