Este artigo descreve o método de implementação do StringBuffer em JavaScript. Compartilhe para sua referência. Os detalhes são os seguintes:
A implementação da classe JavaScript StringBuffer é construir uma classe StringBuffer através do protótipo, e o código é o seguinte:
function stringbuffer () {this .__ strings__ = new Array ();} stringbuffer.prototype.append = function (str) {this .__ strings __. push (str);}; stringbuffer.prototype.toString = function () {return this .__ Strings __. JON (");exemplo:
<html> <head> <title> teste </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 __. Junção (""); }; função testStringBuffer () {var date1 = new Date (); var str; for (var str; 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 tempo:" + (date4 - date3) + "ms"); } </script> </head> <body> <input type = "button" value = "testStringbuffer" onclick = "testStringBuffer ()"/> </body> </html>Espero que este artigo seja útil para a programação JavaScript de todos.