js的StringBuffer類的實現及使用

來源:互聯網
上載者:User

標籤:style   java   使用   os   io   strong   for   art   

  1. <strong>JAVA中有一個StringBuffer類,js中卻沒有下面來自己實現一個簡單的js的StringBuffer類。</strong>  
  1.               //建立一個StringBuffer類 ,此類有兩個方法:一個是append方法一個是toString方法  
  2. function StringBuffer() {  
  3.     this.__strings__ = [];  
  4. };  
  5. StringBuffer.prototype.append = function(str) {  
  6.     this.__strings__.push(str);  
  7. };  
  8. StringBuffer.prototype.toString = function() {  
  9.     return this.__strings__.join(‘‘);  
  10. };  

StringBuffer的使用:

 

  1. //此執行個體的主要是用來拼接字串  
  1. function GnComCard(companymsg) {  
  2.             var buffer = new StringBuffer();//聲明一個對象  
  3.             buffer.append(‘<li>‘);//追加字串,下同  
  4.             buffer.append(‘<div class="com_photo"><a href="#" title="帳戶圖片"><img class="userPhoto" src="‘);  
  5.             buffer.append(companymsg.userPhoto);  
  6.             buffer.append(‘"></img></a></div>‘);  
  7.             buffer.append(‘<div class="com_msg">‘);  
  8.             buffer.append(‘<div><a class="com_comname">‘+ companymsg.companyMasterName+ ‘</a> <span class="com_time">‘  
  9.                     + FormatDate(companymsg.crtime) + ‘</span></div>‘);  
  10.             buffer.append(‘</div><div class="news">‘);  
  11.             if (companymsg.newsId != null) {  
  12.                 buffer.append(‘<a href="‘+companymsg.newsId+‘" title="新聞動態" class="com_newsname">‘+ companymsg.newsTitle + ‘</a>‘);  
  13.             } else {  
  14.                 buffer.append(‘<a href="javascript:void(0)" class="com_newsname">‘+ companymsg.newsTitle + ‘</a>‘);  
  15.             }  
  16.             if (companymsg.newsContent != null) {  
  17.                 buffer.append(‘<div class="com_sh"><span class="toggle">>>展開</span></div>‘);  
  18.                 buffer.append(‘<div class="com_newscontent">‘+ companymsg.newsContent + ‘</div>‘);  
  19.             }  
  20.             buffer.append(‘</div>‘);  
  21.             buffer.append(‘<div class="comment"& gt;<a href="javascript:void(0)" style="float:left" title="追蹤公司">& lt;span class="track"></span></a><input type="text"></input></div>‘);  
  22.             buffer.append(‘</div></li>‘);  
  23.             return buffer.toString();//拼接成一個字串  
  24.         } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.