www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help with this code...Simulating Private Methods.

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    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.

  2. #2
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    152
    If you use this.privateArray or this.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 this 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(){}(); 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
    Last edited by rpg2009; 01-08-2010 at 10:28 PM.

  3. #3
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    152
    Just a simple closure example, not sure if this helps.

    PHP Code:
    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. 
    RPG

  4. #4
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by rpg2009 View Post
    Just a simple closure example, not sure if this helps.

    PHP Code:
    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. 
    RPG

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles