zoukankan      html  css  js  c++  java
  • 装饰者模式AOP

    Function.prototype.after=function(fn){
      var _this=this;
      return function(){
          var _q=_this.apply(this, arguments);
          fn.apply(this, arguments);
          return _q;
      };
    };
    
    Function.prototype.before= function(fn){
      var _this=this;
      return function(){
          fn.apply(this, arguments);
          return _this.apply(this, arguments);
      };
    };
    
    function qq(){
      alert('qqq');
    }
    var q1=qq.before(function(){
        alert(1);
      }).after(function(){
        alert(2);
      }).before(function(){
        alert(3);
      });
    
    q1();
    
    ----------------------------------------------------------------------------
    
    var before = function( Fn , beforeFn ){
      return function(){
        beforeFn.apply(this,arguments);
        Fn.apply(this,arguments);
      };
    };
    var a = before(
      function(){ alert(1); },
      function(){ alert(2); }
    );
    
    a = before(a,function(){alert(3);});
    a = before(a,function(){alert(4);});
    a = before(a,function(){alert(5);});
    a = before(a,function(){alert(6);});
    a();

     摘自JavaScript设计模式与开发实践

  • 相关阅读:
    基本内置类型
    多维数组
    数组
    迭代器
    标准库类型 vector
    标准库类型 string
    运算符优先级表
    类型转换
    sizeof 和逗号运算符
    位运算符
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6610445.html
Copyright © 2011-2022 走看看