装饰者模式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设计模式与开发实践

原文地址:https://www.cnblogs.com/cszdsb/p/6610445.html