By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

The interesting question on Closure

dmjpro
100+
P: 2,476
Today i looked at JavaScript closures. There i came across a thing closure comes in when an inner function defined inside an outer function. So when outer function exits and returns the inner function reference then a hidden reference returns along with the reference of inner function.

Now my question is when that hidden reference created? And the hidden reference have the references of all local variables of outer function. Now my second question..
If any variables local to a loop scope are also being referenced by hidden pointer. But those variables get destroyed after loop scope ends up, how it gets handled?

Now have a look at code snippet....
Expand|Select|Wrap|Line Numbers
  1. function outer(){
  2. var var1 = some_value;
  3. var inner = function(){some_code;}
  4. return inner;
  5. }
  6.  
Here when inner returns then a closure of outer's local variables get created.
so the reference of inner variable being referenced by hidden pointer or only variable var1?

I think experts can get my point of view.
Please answer these questions!
Aug 6 '08 #1
Share this Question
Share on Google+
4 Replies


rnd me
Expert 100+
P: 427
Today i looked at JavaScript closures. There i came across a thing closure comes in when an inner function defined inside an outer function. So when outer function exits and returns the inner function reference then a hidden reference returns along with the reference of inner function.
1. loops don't have scope in javascript.
2. your example function does not produce a closure, no variable name is reused to be preserved.


here is a simple closure example, showing how functions preserve scope.

Expand|Select|Wrap|Line Numbers
  1.     var name = "bob";
  2.  
  3.   function demoClosures(){
  4.     var name = "fred";
  5.     setTimeout(  "alert('not closed: hello '     +   name )"  , 100);
  6.     setTimeout(  function(){ alert('closed: hello '+ name )} , 500);
  7.   }
  8.  
  9.   demoClosures();
notice how name is (en)closed in the function-based setTimeout.
Aug 6 '08 #2

dmjpro
100+
P: 2,476
1. loops don't have scope in javascript.
2. your example function does not produce a closure, no variable name is reused to be preserved.


here is a simple closure example, showing how functions preserve scope.

Expand|Select|Wrap|Line Numbers
  1.     var name = "bob";
  2.  
  3.   function demoClosures(){
  4.     var name = "fred";
  5.     setTimeout(  "alert('not closed: hello '     +   name )"  , 100);
  6.     setTimeout(  function(){ alert('closed: hello '+ name )} , 500);
  7.   }
  8.  
  9.   demoClosures();
notice how name is (en)closed in the function-based setTimeout.
OK i checked that there is no scope inside a function .... :-)
Actually i didn't mention that the variable used by my inner function code.
Anyway i can understand and thanks for sharing your knowledge.
Now my question is that which name will be used in inner function; the global one or the enclosed name?
Please explain...
Aug 6 '08 #3

gits
Expert Mod 5K+
P: 5,343
in your first timeout the construct is evaled later out of the scope of the current function from where it is called so it will alert 'bob', which is your global variable ... the second one closures the value of the local variable name and will alert 'fred' ... ok? :)
Aug 6 '08 #4

rnd me
Expert 100+
P: 427
OK i checked that there is no scope inside a function .... :-)
you should check again. functions are the about only thing that has scope in javascript. 1. 2. 3.


to answer your question though, the closest, most-local, variable will be used. it will creep outward looking for matches of the name until get to window (global). .prototype props and methods are checked only if no other match is found.
Aug 6 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.