View Single Post
Old 04-23-2009, 02:46 PM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default closures and memory leaks

JS closures is still a little bit beyond my understanding but in this article I am reading, I just don't see what the author is pointing out...

He says:

Quote:
Even defining the simplest of functions can create closures:
Code:
var x = 5;
var n = function(){
    y=10;
    return y;
}
Quote:
It doesn't seem like we are creating a closure here, but we are. Why? When we create our function it gets a reference to all variables in its current scope, so we are creating a new reference to our x variable.


Now I keep staring at that code, and I don't see what he's talking about? The function has only one variable in it's scope, and that's the Y variable in which he returns it.

How does that have any affect at all on the X variable?

I am reading it from here:

http://www.htmlgoodies.com/primers/j...le.php/3606701
allworknoplay is offline  
Reply With Quote