R
RobG
I've seen many comments that the following creates a closure:
var firstNameValue = (function(elementId) {
var firstName = document.getElementById(elementId);
return firstName.value;
})("firstName");
I just don't see it. There is only one function, an anonymous
function, and it only has access to its own and global variables. It
gets a reference to a DOM element and returns the string value of one
of its properties. Then the function ends, there is nothing
referencing it and it has no references to variables in its outer
scope.
The only thing it references outside its scope is a solitary DOM
element. Is the inference that because IE makes element ids into
global variables that somehow creating a reference within the
anonymous function creates a closure? If that is true, then *every*
function that gets an element reference using getElementById creates a
closure every time it's called.
Can someone enlighten me?
var firstNameValue = (function(elementId) {
var firstName = document.getElementById(elementId);
return firstName.value;
})("firstName");
I just don't see it. There is only one function, an anonymous
function, and it only has access to its own and global variables. It
gets a reference to a DOM element and returns the string value of one
of its properties. Then the function ends, there is nothing
referencing it and it has no references to variables in its outer
scope.
The only thing it references outside its scope is a solitary DOM
element. Is the inference that because IE makes element ids into
global variables that somehow creating a reference within the
anonymous function creates a closure? If that is true, then *every*
function that gets an element reference using getElementById creates a
closure every time it's called.
Can someone enlighten me?