老妖的博客
          現實的中沒有幾個人能夠真為對方去死,甚至山盟海誓很快就會在金錢面前變的微不足道,這才是生活。沒有永遠的愛,除了你的父母對你,當然也就沒有永遠的恨,更沒有永遠的痛,時間是最好的治療大師,它會很快撫平你心靈上累累的傷痕。很多年以后你想起來時,那些在你生命中洶涌來往的人群至多是個模糊的影子或者毫無意義的名字
          posts - 105,  comments - 171,  trackbacks - 0
          aop_demo.html
          <script src="Aspects.js"></script>
          <script>
          // Business logic
          function makeGreeting(text) {
              
          return "Hello " + text + "!";
          }
          alert(makeGreeting(
          "world")); // Hello world!
          </script>
          <script>
          // Advices
          function aopizeAdvice(args) {
              args[
          0= "AOP " + args[0];    return args;
          }
          function shoutAdvice(result) {
              
          return result.toUpperCase();
          }
          function ciaoAdvice() {
              
          return "Bye-bye!";
          }
          </script>
          <script>
          // Weaver and fun
          Aspects.addBefore(this"makeGreeting", aopizeAdvice);
          alert(makeGreeting(
          "world")); // Hello AOP world!
          Aspects.addAfter(this"makeGreeting", shoutAdvice);
          alert(makeGreeting(
          "world")); // HELLO AOP WORLD!
          Aspects.addAround(this"makeGreeting", ciaoAdvice);
          alert(makeGreeting(
          "world")); // Bye-bye!
           </script>
          Aspects.js:
          Aspects = new Object();
          Aspects.addBefore = function(obj, fname, before) {
              var oldFunc = obj[fname];
              obj[fname] = function() {
                  return oldFunc.apply(this, before(arguments, oldFunc, this));
              };
          };
          Aspects.addAfter = function(obj, fname, after) {
              var oldFunc = obj[fname];
              obj[fname] = function() {
                  return after(oldFunc.apply(this, arguments), arguments, oldFunc, this);
              };
          };
          Aspects.addAround = function(obj, fname, around) {
              var oldFunc = obj[fname];
              obj[fname] = function() {
                  return around(arguments, oldFunc, this);
              };
          };
          aop_demo.html

           

          script src="Aspects.js"></script>
          <script>
          //Business logic
          function Document(){}
              Document.prototype 
          = {
              _id: 
          0,
              _name: '',
               name: 
          function() {
                  
          return this._name;
              },
              id: 
          function() {
                  
          return this._id;
              },
              save: 
          function() {
                  
          return true;
              },
              open: 
          function(id) {
                  
          this._id = id;
                  
          this._name = 'Ajax on AOP steroids'
                  
          return true;
              }
          }
          function openDocument(id) {
              
          var doc = new Document();
              
          try {
                  doc.open(id);
              } 
          catch(e) {
                  alert(e);
                  
          return;
              }
              
          // Update icons and other user elements affected
              alert("Doc id: " + doc.id());
              
          return doc;
          }
          </script>
          <script>
          //Advices
          function Lockable(){}
          Lockable.prototype 
          = {
              _locked: 
          false,
              locked: 
          function() {
                  
          return this._locked;
              }
          }
          function lockOnOpen() {
              
          // Lock this object         
              // If we didn't succeed 
              throw (new Error ("Failed locking " + this._name));

              
          // The object is locked
              this._locked = true;

              
          var ret = proceed();     

              
          return ret;
          }
          </script>
          <script>
          // Weaver and fun
          try {
              Aspects.addIntroduction(Lockable, Document);
              Aspects.addAround(lockOnOpen, Document, 
          "open");
              openDocument ( 
          "test");
          catch(e) {
            alert(e);
          }
          </script>
          Aspects.js
          InvalidAspect = new Error("Missing a valid aspect. Aspect is not a function.");
          InvalidObject = new Error("Missing valid object or an array of valid objects.");
          InvalidMethod = new Error("Missing valid method to apply aspect on.");
          Aspects = new Object();
          Aspects._addIntroduction = function(intro, obj) {
              for (var m in intro.prototype) {
                  obj.prototype[m] = intro.prototype[m];
              }
          }
          Aspects.addIntroduction = function(aspect, objs) {
              var oType = typeof(objs);
              if (typeof(aspect) != 'function') throw(InvalidAspect);
              if (oType == 'function') {
                  this._addIntroduction(aspect, objs);
              } else if (oType == 'object') {
                  for (var n = 0; n 
          < objs.length; n++) {
                      this._addIntroduction(aspect, objs[n]);
                  }
              } else {
                  throw InvalidObject;
              }
          }
          Aspects.addBefore 
          = function(aspect, obj, funcs) {
              var fType 
          = typeof(funcs);
              
          if (typeof(aspect) != 'function') throw(InvalidAspect);
              if (fType !
          = 'object') funcs = Array(funcs);
              
          for (var n = 0; n < funcs.length; n++) {
                  var fName 
          = funcs[n];
                  
          var old = obj.prototype[fName];
                  
          if (!old) throw InvalidMethod;
                  obj.prototype[fName] 
          = function() {
                      aspect.apply(this, arguments);
                      return old.apply(this, arguments);
                  }
              }
          }
          Aspects.addAfter 
          = function(aspect, obj, funcs) {
              if (typeof(aspect) !
          = 'function') throw InvalidAspect;
              if (typeof(funcs) !
          = 'object') funcs = Array(funcs);
              
          for (var n = 0; n < funcs.length; n++) {
                  var fName 
          = funcs[n];
                  
          var old = obj.prototype[fName];        if (!old) throw InvalidMethod;
                  obj.prototype[fName] 
          = function() {
                      var args 
          = old.apply(this, arguments);
                      return ret 
          = aspect.apply(this, Array(args, null));
                  }
              }
          }
          Aspects._getLogic 
          = function(func) {
              var oSrc 
          = new String(func);
              var nSrc 
          = '';
              
          var n = 0;
              
          while (oSrcn) {
                  if (oSrc[n] 
          == '\\n' || oSrc[n] == '\\r') nSrc[n++] += ' ';
                  else nSrc +
          = oSrc[n++];
              
          }
              n 
          = 0;
              
          while (nSrc[n++] != '{');
              
          nSrc = nSrc.substring(n, nSrc.length - 1);
              return nSrc;
          }
          Aspects.addAround 
          = function(aspect, obj, funcs) {
              if (typeof(aspect) !
          = 'function') throw InvalidAspect;
              if (typeof(funcs) !
          = 'object') funcs = Array(funcs);
              
          var aSrc = this._getLogic(aspect);
              
          for (var n = 0; n < funcs.length; n++) {
                  var fName 
          = funcs[n];
                  
          if (!obj.prototype[fName]) throw InvalidMethod;
                  var oSrc 
          = 'var original = ' + obj.prototype[fName];
                  var fSrc 
          = oSrc + aSrc.replace('proceed();','original.apply(this, arguments);');
                  obj.prototype[fName] 
          = Function(fSrc);
              
          }
              return true;
          }
          posted on 2005-12-22 12:39 老妖 閱讀(829) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2005年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          隨筆分類(48)

          隨筆檔案(104)

          好友鏈接

          我的豆瓣

          積分與排名

          • 積分 - 221200
          • 排名 - 257

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 监利县| 宝应县| 云安县| 清镇市| 商都县| 沧源| 鹤岗市| 南阳市| 清新县| 且末县| 石泉县| 安化县| 锦屏县| 余江县| 姜堰市| 永登县| 普宁市| 繁峙县| 南充市| 额尔古纳市| 凤阳县| 庆阳市| 德安县| 河东区| 奉新县| 临夏市| 阿拉善右旗| 中卫市| 冀州市| 常德市| 成都市| 工布江达县| 阿拉善右旗| 衡山县| 马山县| 宁乡县| 新安县| 西峡县| 瑞昌市| 库伦旗| 松桃|