Skip to content

Instantly share code, notes, and snippets.

@hehongwei44
Created August 21, 2014 11:49
Show Gist options
  • Save hehongwei44/fe71f10e4d2d9295aeab to your computer and use it in GitHub Desktop.
Save hehongwei44/fe71f10e4d2d9295aeab to your computer and use it in GitHub Desktop.
通过数组,拓展字符串拼接容易导致性能的问题。
function StringBuffer() {
this.__strings__ = new Array();
}
StringBuffer.prototype.append = function (str) {
this.__strings__.push(str);
return this;
}
StringBuffer.prototype.toString = function () {
return this.__strings__.join("");
}
var buffer = new StringBuffer();
buffer.append("Hello ").append("javascript");
var result = buffer.toString();
alert(result); //Hello javascript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment