Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question help newb understand nested functions and objects

    This is a little hard to explain so I'm posting an example.

    Code:
    //NESTED FUNCTION I'M USING TO DEMONSTRATE MY QUESTION
    function foo(a) {
    this.x = 5;
    
    //USED TO SET THE VALUE OF X
    if( a == 0 ) 
       return function(z) {
          return x = z;
       }
    
    //USED TO RETRIEVE THE VALUE OF X
    else if( a == 1 )
       return function() {
          return x;
       }
    
    }
    
    
    //MAKES setX THE SETTER FUNCTION
    var setX = foo(0);
    //MAKES getX THE GETTER FUNCTION
    var getX = foo(1);
    
    print( "setX(25), not an instance: " + setX( 25 ) );
    print( "getX(), not an instance: " + getX() );
    
    print( "" );
    //CREATES AN OBJECT INSTANCE USING FOO AS CONSTRUCTOR
    var q = new foo();
    print( "value of x in object instance: " + q.x );
    print( "" );
    
    print( "setX(25), not an instance: " + setX( 25 ) );
    print( "getX(), not an instance: " + getX() );

    This code will output:
    Code:
    setX(25), not an instance: 25
    getX(), not an instance: 25
    
    value of x in object instance: 5
    
    setX(25), not an instance: 25
    getX(), not an instance: 25


    Observations:
    It seems like this.x actually has two meanings.
    1)x becomes a member of the "Function" object foo().
    2)x becomes a part of the constructor for prototype class foo.

    But why then does x revert back to the original value of 5 when I use foo as a constructor? Does javascript automatically save the original value on creation for a reason?


    What is going on behind the scenes to make this happen? Is this behavior part of an ontological model that makes sense?


    Similarly, if I change "this.x" to "var x" I can access the value of x but I can't change it. Not that that I should be able to, the syntax "var x" doesn't make x a member of foo anyway. But I'm still having trouble classifying the relationship "var x" has to the function. Anyways this is more of a tangent. My main question is above but if anyone has something to say about this, I'd be interested to hear it.

    It seems like all these behaviors have rules to them, but there is no conceptual model to think through that guide these behaviors. Or maybe I'm just ignorant. Enlighten me.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,026
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Let me disabuse you, quickly. My mods to your code are in red:
    Code:
    <script type="text/javascript">
    
    function print( s ) { document.write(s + "<br/><br/>") ; }
    
    // a foo constructor:
    function foo(a) {
        this.x = 5;
    
        //USED TO SET THE VALUE OF X
        if( a == 0 ) 
            return function(z) {
                    return x = z;
                }
    
        //USED TO RETRIEVE THE VALUE OF X
        else if( a == 1 )
            return function() {
                    return x;
                }
    
    }
    
    
    //MAKES setX THE SETTER FUNCTION
    var setX = foo(0);
    //MAKES getX THE GETTER FUNCTION
    var getX = foo(1);
    
    print( "setX(25), not an instance: " + setX( 25 ) );
    x = "bananas foster";
    print( "getX(), not an instance: " + getX() );
    
    print( "" );
    
    //CREATES AN OBJECT INSTANCE USING FOO AS CONSTRUCTOR
    var q = new foo();
    print( "value of x in object instance: " + q.x );
    print( "" );
    
    print( "setX(25), not an instance: " + setX( 25 ) );
    x = Math.random();
    print( "getX(), not an instance: " + getX() );
    
    </script>
    Enough said? You just *THOUGHT* that the variable x you were using in setX() and getX() was the same one used in the constructor. It's not. It's the global variable x.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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