|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Enthusiast
![]() Join Date: Nov 2008
Location: Thailand
Posts: 74
|
Clarification on closures?
Hi,
Trying slowly but surely to get to grips with closures. My question really is regarding the differences between function declaration and invocation. If I look at Example 1(below), it seems to me that when the function is called the Closure1 inner function is only be declared at that time. Rather than invoked. From what I've read when an inner function is declared a function object is created and it is given the scope of the context in which it is declared i.e. in this case Example1. So does that mean the scope chain is assigned to it's scope property, and that the scope chain points to the Example1's activation/variable object? Does it also mean that because of this reference Example1's activation object is saved from garbage collection? I think my real question is about the preservation of the outer function's variables. In this regard does it make a difference whether an inner function is invoked or declared? It seems to me the answer is no. Additionally I've tried to breakdown the script example with comments at the bottom. If my understanding is way off I would love to know. Hopefully I'm not talking too much nonsense here. Would like to be 100% on this. Thanks RLM Java Code:
|
|
|
|
|
|
#2 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2007
Location: North Yorkshire, UK
Posts: 38
|
What a complex question just trying to get my head around it.
As far as Example 1 is concerned. I believe each element of the array is a function, the same code but essentially a different function. When the assignment is done then you are declaring the function. Only when the array element is used does the function is executed. The function returns the current value of the variable num. Normally, num would be just local to that loop but the function is effectively part of the loop. In example 2 I believe your are declaring a function with a argument numVal and at the same time invoking in because of the bracketing with an argument (num). That function returns the internal function (Closure 2). This time Closure 2 references the numVal argument. Since the outer function is called on each iteration of the loop the inner function will reference the value at that time. As with example 1 each element of the array is a function, the code might be same but the local variables are different. Sorry if this is saying the same as you but in different words. |
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2008
Posts: 4,767
|
You're on the right track, but let me emphasize something. The point is that you're creating a copy of the value. This is because the number is a primitive type, not a reference type, and calling a function with a primitive type as one of its arguments, will create a copy of that arguments value.
|
|
|
|
|
|
#4 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2007
Location: North Yorkshire, UK
Posts: 38
|
Any function is made up of code and local data. In the example 1 they is just code; the only piece of data isn't local to the function. In Example 2 there is both code and data, in that the arguments of the function call are the data. Each array element will have the same code but different data.
This might we seen more easily if Example 2 had been Code:
function Example2(){
var a = [];
for (var num=0; num<10;num++){
a[num] = (function(numVal){// Closure 1
var mynum = numVal;
return function(){// Closure 2
return mynum;
}
})(num);
}
return a;
}
I haven't check this out - so please correct me if I am wrong. |
|
|
|
|
|
#5 |
|
SitePoint Enthusiast
![]() Join Date: Nov 2008
Location: Thailand
Posts: 74
|
Philip thanks for the help, really appreciated. Will have to give it a bit more thought though regarding your 'data' and 'code' explanation. There's so many terms associated with functions and there invocation/declaration that it's a bit hard to keep track.
crmalibu, that makes sense. Thank you. Therefore we get a different copy for each iteration of the loop. This would be a different story had we passed a reference type as an argument instead. Am I still right in saying that the inner most closure creates references to these copies? In that it looks up numVal in it's own scope, doesn't find it then has to reference it from next along the chain closure1? Thanks RLM |
|
|
|
|
|
#6 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2008
Posts: 4,767
|
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 09:22.










Linear Mode
