var StringBuilder = function(){
this.cache = [];
if(arguments.length)this.append.apply(this,arguments);
}
StringBuilder.prototype = {
prepend:function(){
this.cache.splice.apply(this.cache,[].concat.apply([0,0],arguments));
return this;
},
append:function(){
this.cache = this.cache.concat.apply(this.cache,arguments);
return this;
},
toString:function(){
return this.getString();
},
getString:function(){
return this.cache.join('');
}
}
<script type="text/javascript" src="stringbuilder.js"></script>
<script type="text/javascript">
var sb = new StringBuilder('my',' friend,');
sb.append('I');
sb.append(' am');
sb.append(' Robin',' Chen.');
sb.prepend('hi,');
sb.prepend('Hi,','hi,');
alert(sb);
alert(sb.getString());
</script>
this.cache = [];
if(arguments.length)this.append.apply(this,arguments);
}
StringBuilder.prototype = {
prepend:function(){
this.cache.splice.apply(this.cache,[].concat.apply([0,0],arguments));
return this;
},
append:function(){
this.cache = this.cache.concat.apply(this.cache,arguments);
return this;
},
toString:function(){
return this.getString();
},
getString:function(){
return this.cache.join('');
}
}
<script type="text/javascript" src="stringbuilder.js"></script>
<script type="text/javascript">
var sb = new StringBuilder('my',' friend,');
sb.append('I');
sb.append(' am');
sb.append(' Robin',' Chen.');
sb.prepend('hi,');
sb.prepend('Hi,','hi,');
alert(sb);
alert(sb.getString());
</script>