/    Sign up×
Community /Pin to ProfileBookmark

Help with this code…Simulating Private Methods.

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rpg2009Jan 09.2010 — If you use [B]this.[/B]privateArray or [B]this.[/B]privateAppend in your object you create a public property or a public method. So for instance the property can then be set like this StringBuilder.privateArray[0] = "Super";

You aren't using [B]this[/B] and therefore the properties aren't accessible outside of the function. We are talking about function scope here. Only inner functions have access to their outer/ancestor function's properties. Think child to parent access, not the other way round.

However what you've done to get round this is formed a closure by returning add:... and value:.... to Stringbuilder. (These getter and setter methods have private access to your properties).

add: and value: are inner functions and therefore as already mentioned have access to the outer function's properties and methods. In this case privateArray and privateAppend. Because they've been returned to stringbuilder it forms a permanent reference preventing the properties from being chucked out for garbage collection.

The reason it iterates is simple 'privateArray.length'. Each time something is added to privateArray the length value increases by 1.

My explanantion may not be a 100&#37; and maybe a tad confusing. You really need to look into topics such as closures, function invocation, scope and references to clearly understand what's going on here.

Lastly the function(){}[B]()[/B]; means it's a self executing anonymous function. It executes immediately returning add: and value: to StringBuilder.

Hopefully someone else will be able to add to or correct my explanation.

RPG
Copy linkTweet thisAlerts:
@rpg2009Jan 09.2010 — Just a simple closure example, not sure if this helps.

[code=php]var closureTest = function (){
var fruit = "apple";
var closure = function(){return fruit;}; // inner function
return closure;
}

// We're outside of the closureTest function so have no access to var fruit
if (typeof fruit == "undefined") alert ("undefined"); // undefined.
// the inner function closure is returned to the variable access
var access = closureTest();
// Now we invoke access. access is our closure and therefore does have access to fruit.
alert (access());// apple. [/code]


RPG
Copy linkTweet thisAlerts:
@ProtomanMtauthorJan 09.2010 — Just a simple closure example, not sure if this helps.

[code=php]var closureTest = function (){
var fruit = "apple";
var closure = function(){return fruit;}; // inner function
return closure;
}

// We're outside of the closureTest function so have no access to var fruit
if (typeof fruit == "undefined") alert ("undefined"); // undefined.
// the inner function closure is returned to the variable access
var access = closureTest();
// Now we invoke access. access is our closure and therefore does have access to fruit.
alert (access());// apple. [/code]


RPG[/QUOTE]



Suprisingly this has happened me understand closures a bit more. So I thank you for that example. But I'm still somewhat confused about the example that I have posted, the reason being that it has so much going on at once. I might just end up skipping it in the book and just come back to it later when I understand it a little more. It's one of those things that really have to be broken down for me to completely understand. I'll be coming back to this thread to re-look at your post periodically as I progress through the book and understand the concepts a little more clearly.
×

Success!

Help @ProtomanMt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...