亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript實(shí)現(xiàn)Java中StringBuffer的方法

 更新時(shí)間:2015年02月09日 11:24:22   作者:wilsonyun  
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Java中StringBuffer的方法,實(shí)例分析了StringBuffer類的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了JavaScript實(shí)現(xiàn)Java中StringBuffer的方法。分享給大家供大家參考。具體如下:

Javascript StringBuffer類的實(shí)現(xiàn)是通過(guò)prototype構(gòu)造一個(gè)StringBuffer類,代碼如下:

function StringBuffer() {
  this.__strings__ = new Array();
}

StringBuffer.prototype.append = function(str) {
  this.__strings__.push(str);
};

StringBuffer.prototype.toString = function() {
  return this.__strings__.join("");
};

例子:

<html>
<head>
<title>test</title>
<script type="text/javascript">
    function StringBuffer() {
      this.__strings__ = new Array();
    }
    StringBuffer.prototype.append = function(str) {
      this.__strings__.push(str);
    };
    StringBuffer.prototype.toString = function() {
      return this.__strings__.join("");
    };

    function testStringBuffer(){
       var date1 = new Date();
       var str;
       for( var i=0; i<10000; i++){
         str += "text";
       }
       var date2 = new Date();
       document.writeln("Sting use time:"+ (date2 - date1) +"ms");

       //StringBuffer
       var date3 = new Date();
       var strBuffer = new StringBuffer();
       for(i=0; i<10000; i++){
         strBuffer.append("text");
       }
       strBuffer.toString();
       var date4 = new Date();
       document.writeln("<br/>StringBuffer use time:"+ (date4 - date3) +"ms");
    }
</script>
</head>
<body>
   <input type="button" value="testStringBuffer" onclick="testStringBuffer()"/>
</body>
</html>

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論