The realization and use of JS's StringBuffer class

Source: Internet
Author: User

    1. There is a StringBuffer class in <strong>java, JS is not the following to achieve a simple JS StringBuffer class. </strong>
  1. //Create a StringBuffer class with two methods: One is the Append method, the other is the ToString method
  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. };

Use of StringBuffer:

    1. The main use of this instance is to stitch strings
  1. function Gncomcard (companymsg) {
  2. var buffer = new StringBuffer (); Declaring an object
  3. Buffer.append (' <li> '); Append string, same as
  4. Buffer.append (' <div class= ' Com_photo "><a href=" # "title=" user picture ">
  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=" News "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 ' >>> expand </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= "tracking Company"  >& lt;span class= "track" ></span></a><input type= "text" ></input></div> ");
  22. Buffer.append (' </div></li> ');
  23. return buffer.tostring (); //stitching into a string
  24. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.