This is an old blog. My new blog is moved here.

JavaScript ASP.NET like StringBuilder using the module pattern

12/29/2009

Here is a nicel ittle utility that will make your JavaScript code much cleaner while keeping the performance high: C#-like string builder.

Note that this "class" is using Douglas Crockford's module pattern, so not "new" operator is needed when creating an instance.

NameSpace.util = {
stringBuilder : function (){

/* StringBuilder
------------------------------

//Description:
works like a regular stringbuilder

//Example:
var sb = NameSpace.util.stringBuilder();
sb.append("hi ");
sb.append("dave");
sb.append(" !");
console.log(sb.toString());
*/

var s = [];
return {
// appends
append : function (v){
if (v){
s.push(v);
}
},
// clears
clear : function (){
s.length = 1;
},
// converts to string
toString : function (){
return s.join("");
}
}
}
}

Responses to The JavaScript ASP.NET like StringBuilder using the module pattern