...

View Full Version : help newb understand nested functions and objects



dude
11-16-2010, 08:13 AM
This is a little hard to explain so I'm posting an example.


//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:

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.

Old Pedant
11-16-2010, 07:02 PM
Let me disabuse you, quickly. My mods to your code are in red:


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum