469,329 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

The interesting question on Closure

dmjpro
2,476 2GB
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
4 1205
rnd me
427 Expert 256MB
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
2,476 2GB
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
5,390 Expert Mod 4TB
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
427 Expert 256MB
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.

Similar topics

27 posts views Thread by Ted Lilley | last post: by
9 posts views Thread by Mikito Harakiri | last post: by
7 posts views Thread by Csaba Gabor | last post: by
9 posts views Thread by User1014 | last post: by
reply views Thread by Gerard Brunick | last post: by
11 posts views Thread by Huayang Xia | last post: by
4 posts views Thread by LAN MIND | last post: by
4 posts views Thread by JavascriptProgrammer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.