zoukankan      html  css  js  c++  java
  • javascript AOP 实现,ajax回调函数使用比较方便

    function actsAsDecorator(object) {
      object.setupDecoratorFor = function(method) {
        if (! ('original_' + method in object) ) {
          object['original_' + method] = object[method];
          object['before_' + method]   = [ ];
          object['after_' + method]    = [ ];
          object[method] = function() {
            var i;
            var b = this['before_' + method];
            var a = this['after_'  + method];
            var rv;
            for (i = 0; i < b.length; i++) {
              b[i].call(this, arguments);
            }
            rv = this['original_' + method].apply(this, arguments);
            for (i = 0; i < a.length; i++) {
              a[i].call(this, arguments);
            }
            return rv;
          }
        }
      };
      object.before = function(method, f) {
        object.setupDecoratorFor(method);
        object['before_' + method].unshift(f);
      };
      object.after = function(method, f) {
        object.setupDecoratorFor(method);
        object['after_' + method].push(f);
      };
    }
    /**
    Invoking
    */
        function Test(){
            this.say1 =  function(s){
                alert(s);
            }
            this.say2 =  function(s){
                alert(s);
            }
        }
    var t = new Test();
    actsAsDecorator(t);
        t.before("say1",beforeHander);
        t.after("say2",afterHander);
        test();
    
    
    
    

  • 相关阅读:
    消息队列学习
    php加密技术
    mysql 数据库优化
    mysql 数据库设计
    mysql 存储引擎
    用python计算圆周率并用进度条并显示计算进度
    关于Turtle库的学习笔记
    Python第一周作业使用turtle库绘图
    turtle绘图的例子
    六边形的绘制
  • 原文地址:https://www.cnblogs.com/wblade/p/1882652.html
Copyright © 2011-2022 走看看