| Arhip ( @ 2006-08-21 14:58:00 |
| Entry tags: | programming |
Efficient string concatenation in JS
/***************************************
* Function : StringBuffer object
* Methods :
* append(string)
* toString() - returns string
* length() - returns count of array elements
* clear() - removes all from array
* Constructor : var buf = new StringBuffer();
* buf.append("a");
* buf.append("b");
* buf.append("c");
* buf.toString(); // returns "abc"
****************************************
function StringBuffer() {
this.buffer = [];
}
StringBuffer.prototype.append = function append(string) {
this.buffer.push(string);
return this;
};
StringBuffer.prototype.toString = function toString() {
return this.buffer.join("");
};
StringBuffer.prototype.length = function length() {
return this.buffer.length;
};
StringBuffer.prototype.clear = function clear() {
this.buffer.splice(0,this.length());
return this;
};