Where The Streets Have No Name

actsAsAspect.js 본문

Developement/Web

actsAsAspect.js

highheat 2007. 3. 21. 21:29
/*
 * 출처 : http://beppu.lbox.org/articles/2006/09/06/actsasaspect
 */
functionactsAsAspect(object) {   
  object.yield = null;
  object.rv    = { };

  object.before  = function(method, f) {
    var original = eval("this." + method);
    this[method] = function() {
      f.apply(this, arguments);
      return original.apply(this, arguments);
    };
  };

  object.after   = function(method, f) {
    var original = eval("this." + method);
    this[method] = function() {
      this.rv[method] = original.apply(this, arguments);
      return f.apply(this, arguments);
    }
  };

  object.around  = function(method, f) {
    var original = eval("this." + method);
    this[method] = function() {
      this.yield = original;
      return f.apply(this, arguments);
    }
  };
}