TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-23-2009, 02:46 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 646
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
Old 04-23-2009, 04:59 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-23-2009, 05:39 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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.
Sure, there's only one variable used (aside: not declared) inside the function but the variable x is still available to be used. For example:

JavaScript Code:
var x = 5;
var n = function(){
    var y = 10;
    return x + y;
};

// … some time later
x = 113;
alert(n()); // 123
 
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 04-24-2009, 11:36 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:19 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design