• 望五月

    Tag:
  • 标题党too。

    今天打球输了,郁闷。第一次打系列赛以全负战绩结束,丫的。

    以后得加强体能训练,下半场眼睁睁地看对方抢篮板。

    呃,好像叫上宿舍几个来的更实在些,丫,切了这群小样的。

    好了,罚自己洗澡打牌去。

  • 咳,最大的好处就是不用修改原来的js代码,可以对方法新增一些功能,比如,这个例子中,判断
    文件文件是否已经被打开了,即锁定了。enjoy it.
    //test.html
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
            <script language="JavaScript" src="aopFramework.js"></script>
            <script language="JavaScript">
               
               
                function Documents(){};
                Documents.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 = 'test Open';
                        return true;
                        }
                    }
                function openDocument(id){
                        var doc = new Document();
                        try{
                            document.open(id);
                            }
                            catch(e){
                                alert(e);
                                return;
                                }
                        alert("Doc id: " + doc.id());
                        return doc;
                }
                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));
                    if(this._locked == true){
                        throw (new Error ("Failed locking " + this._name));
                    }
                  // The object is locked
                 this._locked = true;
                 alert("locked");
                 
                }
                function allTest(){
                        try {
                             //增加对象的属性,prototype
                             Aspects.addIntroduction(Lockable, Documents);
                             //增加对象方法的预处理
                          Aspects.addBefore(lockOnOpen, Documents, "open");
                        }
                        catch(e)
                        {
                          alert(e);
                        }
                        var doc = new Documents();
                      try {
                        doc.open(222);
                      }
                      catch(e)
                      {
                         
                        alert(e);
                      }
                   
                      // Update icons and other user elements affected
                      alert("Doc id: " + doc.id());
                      try {
                        doc.open(222);
                      }
                      catch(e)
                      {
                         
                        alert(e);
                      }
                   
                      // Update icons and other user elements affected
                      alert("Doc id: " + doc.id());
                     
                    }
               
                </script>
            </head>
           
        <body onload="allTest();">
            </body>
    </html>

    //aopFramework.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 (oSrc[n])
        {
          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;
    }
  •        呼,需求的重要性阿,nnd。受不了了。事情怎么都是赶到一起了呢。

    好吧,深呼吸,“阿,世界多么美好啊~~~~~~”。

        btw,我人品问题,用不了沙茶。