< html > < head > < title ></ title > </ head > < body > < script type = " text/javascript " > function StringBuffer() ... { this._strings = []; } StringBuffer.prototype.append = function(str) ... { this._strings.push(str); } StringBuffer.prototype.toString = function() ... { return this._strings.join(""); } // 实际例子 var buffer = new StringBuffer(); buffer.append( " 123 " ); buffer.append( " 456 " ); alert(buffer.toString()); </ script > < script type = " text/jscript " > // 对比运行效率 var d1 = new Date(); var str = "" ; for (var i = 0 ; i < 10000 ; i ++ ) ... { str += "text"; } var d2 = new Date(); document.write( " 用加好连接; " + (d2.getTime() - d1.getTime()) + " 毫秒 " ); var oBuffer = new StringBuffer(); d1 = new Date(); for (var i = 0 ;i < 10000 ;i ++ ) ... { oBuffer.append("text"); } var sResult = buffer.toString(); d2 = new Date(); document.write( " <br />用StringBuffer连接 " + (d2.getTime() - d1.getTime()) + " 毫秒 " ); </ script > </ body > </ html >