Cet article décrit la méthode d'implémentation de StringBuffer dans JavaScript. Partagez-le pour votre référence. Les détails sont les suivants:
L'implémentation de la classe JavaScript StringBuffer est de construire une classe StringBuffer via le prototype, et le code est le suivant:
fonction StringBuffer () {this .__ Strings__ = new Array ();} stringBuffer.prototype.append = function (str) {this .__ cordes __. push (str);}; stringBuffer.prototype.tostring = function () {return this .__ Strings __. join (");};exemple:
<html> <éad- head> <ititle> test </title> <script type = "text / javascript"> function stringBuffer () {this .__ Strings__ = new Array (); } Stringbuffer.prototype.append = fonction (str) {this .__ Strings __. Push (str); }; StringBuffer.prototype.toString = function () {return this .__ Strings __. JOIN (""); }; fonction testStringBuffer () {var date1 = new Date (); var str; pour (var str; i = 0; i <10000; i ++) {str + = "text"; } var date2 = new Date (); Document.Writeln ("Sting Use Heure:" + (Date2 - Date1) + "MS"); // StringBuffer var Date3 = new Date (); var strbuffer = new StringBuffer (); pour (i = 0; i <10000; i ++) {strbuffer.append ("text"); } strbuffer.toString (); var date4 = new Date (); Document.Writeln ("<br/> StringBuffer Use Heure:" + (Date4 - Date3) + "MS"); } </ script> </ head> <body> <input type = "Button" value = "TestStringBuffer" onClick = "TestStringBuffer ()" /> </ body> </html>J'espère que cet article sera utile à la programmation JavaScript de tous.