![]() |
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:
Code:
var x = 5;Quote:
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 |
I assume what he's attempting to say is that both
x and n contain the function which returns y. However, x.toString() appears to show 5 to me, which I'd expect.If x were a reference to n then that'd be expected behaviour, but it isn't, and so I don't understand why he's telling us that.Perhaps somebody else has a better idea. |
Quote:
x is still available to be used. For example:JavaScript Code:
|
He may be trying to show that top level variables are automatically made global (because JS has no linker, so throws it all into a global namespace), thus setting the stage to explain cyclic references in closures and why they happen.
I may be wrong ofc. |
| All times are GMT. The time now is 07:56 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0