<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [譯]擴展JavaScript功能的正確方法

        早上看到《JavaScript 每周導讀》【第三期】一文,里面發現一篇文章(Extending JavaScript – The Right Way),覺得還不錯,翻譯過來跟大家共享,本文并不是逐字逐句進行翻譯,盡量說得通俗易懂。

        原文地址:Extending JavaScript – The Right Way

      以下是譯文

        JavaScript已經內置了很多強大的方法,但有時你需要的某個功能在內置的方法中沒有,我們怎么來優雅地擴展JavaScript功能呢。

        例如我們想增加一個capitalize()方法來實現首字母大寫,通常我們這樣寫:

      if(!String.prototype.capitalize)
      {
          String.prototype.capitalize = function()
          {
              return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
          }
      }

        上面的代碼可以正常使用,但如果在某個地方有下面的代碼:

      var strings = "yay";
      for(i in strings) console.log(i + ":" + strings[i]);

        我們得到的結果是這樣的:

      0: y
      1: a
      2: y
      capitalize: function () { return this.slice(0, 1).toUpperCase() + this.slice(1).toLowerCase(); }

        這顯然不是我們想要的結果,輸出了我們增加的方法的原因是我們增加的方法的enumerable屬性默認為true。

        我們可以通過簡單地把枚舉屬性(enumerable)設置為false避免這個問題,使用defineProperty方法進行功能的擴展:

      if(!String.prototype.capitalize)
      {
          Object.defineProperty(String.prototype, 'capitalize',
          {
             value: function()
             {
                 return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
             },
             enumerable: false
          });
      }

        現在我們再運行這段代碼:

      var strings = "yay";
      for(i in strings) console.log(i + ":" + strings[i]);

        我們得到的結果是:

      0: y
      1: a
      2: y

        要注意的是,用循環沒有輸出的并不代表不存在,我們可以通過下面的代碼查看到定義:

      var strings = "yay";
      console.log(strings.capitalize)

        會輸出:

      function () { return this.slice(0, 1).toUpperCase() + this.slice(1).toLowerCase(); }

        用這種方式擴展JavaScript功能比較靈活,我們可以用這種方式來定義我們自己的對象,并設置一些默認值。

       

        以下是另外幾個擴展方法,你可以在自己的項目中使用:

        String.pxToInt()

        把"200px"這樣的字符串轉換為數字 200 :

      if(!String.prototype.pxToInt)
      {
          Object.defineProperty(String.prototype, 'pxToInt',
          {
              value: function()
              {
                  return parseInt(this.split('px')[0]);
              },
              enumerable: false
          });
      }

       

        String.isHex()

        判斷一個字符串是否是16進制表示的,如"#CCC" 或 "#CACACA"

      if(!String.prototype.isHex)
      {
          Object.defineProperty(String.prototype, 'isHex',
          {
              value: function()
              {
                  return this.substring(0,1) == '#' &&  
                         (this.length == 4 || this.length == 7) && 
                         /^[0-9a-fA-F]+$/.test(this.slice(1));
              },
              enumerable: false
          });
      }

       

        String.reverse()

        字符串反轉:

      if(!String.prototype.reverse)
      {
          Object.defineProperty(String.prototype, 'reverse',
          {
              value: function()
              {
                  return this.split( '' ).reverse().join( '' );
              },
              enumerable: false
          });
      }

       

        String.wordCount()

        統計單詞數量,用空格分開

      if(!String.prototype.wordCount)
      {
          Object.defineProperty(String.prototype, 'wordCount',
          {
              value: function()
              {
                  return this.split(' ').length;
              },
              enumerable: false
          });
      }

       

        String.htmlEntities()

        html標簽如<和>編碼為特殊字符

      if(!String.prototype.htmlEntities)
      {
          Object.defineProperty(String.prototype, 'htmlEntities',
          {
              value: function()
              {
                  return String(this).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
              },
              enumerable: false
          });
      }

       

        String.stripTags()

        去掉HTML標簽:

      if(!String.prototype.stripTags)
      {
          Object.defineProperty(String.prototype, 'stripTags',
          {
              value: function()
              {
                  return this.replace(/<\/?[^>]+>/gi, '');
              },
              enumerable: false
          });
      }

       

        String.trim()

        去掉首尾空格:

      if(!String.prototype.trim)
      {
          Object.defineProperty(String.prototype, 'trim',
          {
              value: function()
              {
                  return this.replace(/^\s*/, "").replace(/\s*$/, "");
              },
              enumerable: false
          });
      }

       

        String.stripNonAlpha()

        去掉非字母字符:

      if(!String.prototype.stripNonAlpha)
      {
          Object.defineProperty(String.prototype, 'stripNonAlpha',
          {
              value: function()
              {
                  return this.replace(/[^A-Za-z ]+/g, "");
              },
              enumerable: false
          });
      }

       

        Object.sizeof()

        統計對象的大小,如{one: “and”, two: “and”}為2

      if(!Object.prototype.sizeof)
      {
          Object.defineProperty(Object.prototype, 'sizeof',
          {
              value: function()
              {
                  var counter = 0;
                  for(index in this) counter++;
                  
                  return counter;
              },
              enumerable: false
          });
      }

      PS

        這種方式擴展JS原生對象的功能還是挺不錯的,但除非必要(項目中用的很多),不建議直接在原生對象上擴展功能,會造成全局變量污染。

        另外,文中的pxToInt()方法是沒什么必要的,JS中的parseInt()可以直接完成這樣的功能:parsetInt("200px")===200

        htmlEntities方法貌似有問題,下面另提供一個:

      if(!String.prototype.htmlEntities)
      {
          Object.defineProperty(String.prototype, 'htmlEntities',
          {
              value: function()
              {
                  var div = document.createElement("div");
                  if(div.textContent){
                      div.textContent=this;
                  }
                  else{
                      div.innerText=this;
                  }
                  return div.innerHTML;
              },
              enumerable: false
          });
      }
      posted @ 2012-04-12 11:01  artwl  閱讀(3295)  評論(7)    收藏  舉報

      個人簡介

      var ME = {
      	"name": "土豆/Artwl",
      	"job": "coding",
      	"languages": [
      		"JS", "HTML",
                      "CSS", "jQuery"
      		"MVC",".NET",
      		"設計模式"
      	],
      	"hobby": [
      		"閱讀", "旅游",
      		"音樂", "電影"
      	]
      }
      
      TOP
      主站蜘蛛池模板: 年轻女教师hd中字3| 亚洲精品人成网线在线| 安义县| 久99久热免费视频播放| 日韩精品人妻中文字幕| 日本伊人色综合网| 一区二区三区无码免费看| 久热爱精品视频线路一| 亚洲 一区二区 在线| 国产裸体永久免费无遮挡| 五月综合网亚洲乱妇久久| 国产一级片内射在线视频| 精品久久综合日本久久网| 翘臀少妇被扒开屁股日出水爆乳 | 日本一区二区在线高清观看| 四房播色综合久久婷婷| 开心激情站一区二区三区| 国产激情一区二区三区不卡| 欧美激情一区二区三区在线| 色一伊人区二区亚洲最大| 欧美成本人视频免费播放| 精品国产成人网站一区在线| 国精产品自偷自偷ym使用方法| 亚洲夂夂婷婷色拍ww47| 黄色亚洲一区二区在线观看| 国产成AV人片久青草影院| 国产无套内射又大又猛又粗又爽| 中文激情一区二区三区四区| 国产成人无码AV大片大片在线观看| 蒙山县| 国产亚洲精品在av| 精品国产AⅤ无码一区二区 | 在线看国产精品自拍内射| 久久一日本道色综合久久| 亚洲午夜精品久久久久久抢| 国产成a人片在线观看视频下载| 亚洲国产精品视频一二区| 中文字幕结果国产精品| 午夜成人无码免费看网站| 在线日韩日本国产亚洲| 疯狂的欧美乱大交|