473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1298
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

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

Similar topics

27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
1
by: Victor Ng | last post by:
Is there a way to preserve the argspec of a function after wrapping it in a closure? I'm looking for a general way to say "wrap function F in a closure", such that inspect.getargspec on the...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
9
by: User1014 | last post by:
I'm a javascript noob and have a question concerning some code I've come across that I'm trying to understand. Here is a snippet of the relevant section: (snip) var closure = this; var xhr =...
0
by: Gerard Brunick | last post by:
Consider: ### Function closure example def outer(s): .... def inner(): .... print s .... return inner .... 5
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
4
by: LAN MIND | last post by:
?
4
by: JavascriptProgrammer | last post by:
In the following code: ----------------------- function get() { return function() { alert(x); } }; function foo(s) { var x = s; this.getX = get();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.