传统的做法是:
var str="hello";
str+="world";
但是每次做赋值都要消耗资源,因此效率低下。
也可以使用如下做法:
var str = new Array;
str[0]="hello";
str[1]="world";
str.join("");
这种方式好了一点儿,但是不能确切的反映出方法的意图。
为了使它更容易理解,可以用StringBuffer类来打包该功能:
function StringBuffer(){
this._strings=new Array;
if(typeof StringBuffer._initialized=="undefined"){
StringBuffer.prototype.append=function(str){
this._strings.push(str);
};
StringBuffer.prototype.toString=function(){
return this._strings.join("");
};
}
}