I’m having trouble understanding this code:
var StringBuilder = function() {
var privateArray = new Array();
function privateAppend(str) {
privateArray[privateArray.length] = str;
}
return {
add: function(str) {
privateAppend(str);
},
value: function() {
return privateArray.join(“”);
}
}
}();
// First we show that the string is empty
document.write(“Our String: ” + StringBuilder.value() + “<br />”); // “Our String: “
StringBuilder.add(“Super”);
StringBuilder.add(“Cala”);
StringBuilder.add(“Frajalistic”);
// Now we display the finished concatenated string
document.write(“Our String: ” + StringBuilder.value() + “<br />”); // “Our String: SuperCalaFrajalistic”
What I’m really having trouble understanding is how the arrays are being played out. I don’t understand how I am iterating through the elements. All I see is that Private array is being initialized to ‘str’. Really confused and would really appreciate your help.