Re: Closures Explained
sasuke <database666@gm ail.comwrites:
I variable is not inherently free or not. A variable occurence in a
part of a program, e.g., function declaration, is free in that
function if the function doesn't contain a declaration of that variable.
Example:
function foo(x) {
return function(y) {
alert(x+y);
}
}
This entire function has only one free variable: "alert".
The variable occurences "x" and "y" are bound by the argument
declarations of the surrounding functions.
The inner function:
function(y) { alert(x+y); }
has two free variables: "alert" and "x".
Notice that the same occurence of "x" can be both free and bound
depending on the scope one consider.
That depends entirely on the javascript implementation.
Best case, nothing remains and the garbage collector claims it all.
Worst case, the garbage collector barfs on the DOM->JS function->DOM
dependencies and collects nothing. That's a memory leak. I believe
IE 6 might do just that.
/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
sasuke <database666@gm ail.comwrites:
On Oct 10, 9:07 pm, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
wrote:
>
So does it mean that even global variables in Javascript are `free
variables' given the fact that they can be used without being declared
or is it that the definition is a loose one?
wrote:
>>
>[snip]
>>
>A closure is a piece of code together with a binding of values to the
>free variables of that code.
>>
>A free variable is one that occur in the code, but is not declared
>there.
>[snip]
>>
>A closure is a piece of code together with a binding of values to the
>free variables of that code.
>>
>A free variable is one that occur in the code, but is not declared
>there.
So does it mean that even global variables in Javascript are `free
variables' given the fact that they can be used without being declared
or is it that the definition is a loose one?
part of a program, e.g., function declaration, is free in that
function if the function doesn't contain a declaration of that variable.
Example:
function foo(x) {
return function(y) {
alert(x+y);
}
}
This entire function has only one free variable: "alert".
The variable occurences "x" and "y" are bound by the argument
declarations of the surrounding functions.
The inner function:
function(y) { alert(x+y); }
has two free variables: "alert" and "x".
Notice that the same occurence of "x" can be both free and bound
depending on the scope one consider.
Also, given the function:
>
---------------------------------->B--------------------------
function attachEvents() {
var divs = document.getEle mentsByTagName( "DIV");
if(!divs) return;
for(var i = 0, maxI = divs.length; i < maxI; ++i) {
var d = divs[i];
d.onclick = function() {
// some complicated processing with a lot of variables
alert("Hello from " + d.id);
}
}
}
window.onload = function() {
attachEvents();
// something complicated
attachEvents();
}
---------------------------------->B--------------------------
>
Will the second invocation of the function `attachEvents' make the
execution context of the first run along with the previously created
function objects eligible for garbage collection or do they need to be
explicitly grounded [set to null]?
>
---------------------------------->B--------------------------
function attachEvents() {
var divs = document.getEle mentsByTagName( "DIV");
if(!divs) return;
for(var i = 0, maxI = divs.length; i < maxI; ++i) {
var d = divs[i];
d.onclick = function() {
// some complicated processing with a lot of variables
alert("Hello from " + d.id);
}
}
}
window.onload = function() {
attachEvents();
// something complicated
attachEvents();
}
---------------------------------->B--------------------------
>
Will the second invocation of the function `attachEvents' make the
execution context of the first run along with the previously created
function objects eligible for garbage collection or do they need to be
explicitly grounded [set to null]?
Best case, nothing remains and the garbage collector claims it all.
Worst case, the garbage collector barfs on the DOM->JS function->DOM
dependencies and collects nothing. That's a memory leak. I believe
IE 6 might do just that.
/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Comment