Doesn't JavaScript Support Closures with Local Variables

Doesn't JavaScript support closures with local variables?

Fixed Jon's answer by adding an additional anonymous function:

function create() {
for (var i = 0; i < 5; i++) {
closures[i] = (function(tmp) {
return function() {
alert("i = " + tmp);
};
})(i);
}
}

The explanation is that JavaScript's scopes are function-level, not block-level, and creating a closure just means that the enclosing scope gets added to the lexical environment of the enclosed function.

After the loop terminates, the function-level variable i has the value 5, and that's what the inner function 'sees'.


As a side note: you should beware of unnecessary function object creation, espacially in loops; it's inefficient, and if DOM objects are involved, it's easy to create circular references and therefore introduce memory leaks in Internet Explorer.

How do JavaScript closures work at a low level?

This is a section of slebetman's answer to the question javascript can't access private properties that answers your question very well.

The Stack:


A scope is related to the stack frame (in Computer Science it's called
the "activation record" but most developers familiar with C or
assembly know it better as stack frame). A scope is to a stack frame
what a class is to an object. By that I mean that where an object is
an instance of a class, a stack frame is an instance of scope.

Let's use a made-up language as an example. In this language, like in
javascript, functions define scope. Lets take a look at an example
code:

var global_var

function b {
var bb
}

function a {
var aa
b();
}

When we read the code above, we say that the variable aa is in scope
in function a and the variable bb is in scope in function b.
Note that we don't call this thing private variables. Because the
opposite of private variables are public variables and both refer to
properties bound to objects. Instead we call aa and bb local
variables. The opposite of local variables are global variables
(not public variables).

Now, let's see what happens when we call a:

a() gets called, create a new stack frame. Allocate space for local
variables on the stack:

The stack:
┌────────┐
│ var aa │ <── a's stack frame
╞════════╡
┆ ┆ <── caller's stack frame

a() calls b(), create a new stack frame. Allocate space for local
variables on the stack:

The stack:
┌────────┐
│ var bb │ <── b's stack frame
╞════════╡
│ var aa │
╞════════╡
┆ ┆

In most programming languages, and this includes javascript, a
function only has access to its own stack frame. Thus a() cannot
access local variables in b() and neither can any other function or
code in global scope access variables in a(). The only exception are
variables in global scope. From an implementation point of view this
is achieved by allocating global variables in an area of memory that
does not belong to the stack. This is generally called the heap. So to
complete the picture the memory at this point looks like this:

The stack:     The heap:
┌────────┐ ┌────────────┐
│ var bb │ │ global_var │
╞════════╡ │ │
│ var aa │ └────────────┘
╞════════╡
┆ ┆

(as a side note, you can also allocate variables on the heap inside
functions using malloc() or new)

Now b() completes and returns, it's stack frame is removed from the
stack:

The stack:     The heap:
┌────────┐ ┌────────────┐
│ var aa │ │ global_var │
╞════════╡ │ │
┆ ┆ └────────────┘

and when a() completes the same happens to its stack frame. This is
how local variables gets allocated and freed automatically - via
pushing and popping objects off the stack.

Closures:


A closure is a more advanced stack frame. But whereas normal stack
frames gets deleted once a function returns, a language with closures
will merely unlink the stack frame (or just the objects it contains)
from the stack while keeping a reference to the stack frame for as
long as it's required.

Now let's look at an example code of a language with closures:

function b {
var bb
return function {
var cc
}
}

function a {
var aa
return b()
}

Now let's see what happens if we do this:

var c = a()

First function a() is called which in turn calls b(). Stack frames
are created and pushed onto the stack:

The stack:
┌────────┐
│ var bb │
╞════════╡
│ var aa │
╞════════╡
│ var c │
┆ ┆

Function b() returns, so it's stack frame is popped off the stack.
But, function b() returns an anonymous function which captures bb
in a closure. So we pop off the stack frame but don't delete it from
memory (until all references to it has been completely garbage
collected):

The stack:             somewhere in RAM:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶┐
│ var aa │ ┆ var bb ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶┘
│ var c │
┆ ┆

a() now returns the function to c. So the stack frame of the call
to b() gets linked to the variable c. Note that it's the stack
frame that gets linked, not the scope. It's kind of like if you create
objects from a class it's the objects that gets assigned to variables,
not the class:

The stack:             somewhere in RAM:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶┐
│ var c╶╶├╶╶╶╶╶╶╶╶╶╶╶┆ var bb ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶┘
┆ ┆

Also note that since we haven't actually called the function c(),
the variable cc is not yet allocated anywhere in memory. It's
currently only a scope, not yet a stack frame until we call c().

Now what happens when we call c()? A stack frame for c() is
created as normal. But this time there is a difference:

The stack:
┌────────┬──────────┐
│ var cc var bb │ <──── attached closure
╞════════╤──────────┘
│ var c │
┆ ┆

The stack frame of b() is attached to the stack frame of c(). So
from the point of view of function c() it's stack also contains all
the variables that were created when function b() was called (Note
again, not the variables in function b() but the variables created
when function b() was called - in other words, not the scope of b()
but the stack frame created when calling b(). The implication is that
there is only one possible function b() but many calls to b() creating
many stack frames).

But the rules of local and global variables still applies. All
variables in b() become local variables to c() and nothing else.
The function that called c() has no access to them.

What this means is that when you redefine c in the caller's scope
like this:

var c = function {/* new function */}

this happens:

                     somewhere in RAM:
┌╶╶╶╶╶╶╶╶╶┐
┆ var bb ┆
└╶╶╶╶╶╶╶╶╶┘
The stack:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐
│ var c╶╶├╶╶╶╶╶╶╶╶╶╶╶┆ /* new function */ ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘
┆ ┆

As you can see, it's impossible to regain access to the stack frame
from the call to b() since the scope that c belongs to doesn't
have access to it.

Javascript: Get access to local variable or variable in closure by its name

I'm not aware of anything built into JavaScript to reference local variables like that (though there probably should be considering all variables are internally referenced by strings).

I'd suggest keeping all your variables in an object if you really need to access by string:

var variables = {
"j": 1
};
alert(variables["j"]);

Update: It kind of bugs me that there's no way to do this like you want. Internally the variable is a mutable binding in the declarative environment records. Properties are bound to the object they're a property of through the object's environment records, but there's actually a way to access them using brackets. Unfortunately, there's no way to access the declarative environment records the same way.

How do JavaScript closures work?

A closure is a pairing of:

  1. A function and
  2. A reference to that function's outer scope (lexical environment)

A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.

Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.

If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.

In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:

function foo() {
const secret = Math.trunc(Math.random() * 100)
return function inner() {
console.log(`The secret number is ${secret}.`)
}
}
const f = foo() // `secret` is not directly accessible from outside `foo`
f() // The only way to retrieve `secret`, is to invoke `f`

My function somehow doesn’t have access to its parent closure & is missing variables. How?

I suspect that the function where you set the breakpoint contains a reference to Backbone, but not Vent or Foo.

Closures are somewhat expensive on the JS runtime. It requires the engine wrap up the object in such a way that it keeps internal references to those variables so they can be resolved correctly at the time the function is executed. So for performance reasons, Chrome (and I suspect most other engines as well) has a tendency to optimize away any closure variables that aren't actually used when the script is compiled. This can lead to some confusing things when debugging, but it's to be expected.

Consider the following example (Note x, y, and z are defined in the scope of the outer function, not in global scope):

window.onload = function() {  var x = 1, y = 2, z = 3;  (function() {    debugger;    x++;  })();}

JavaScript closure inside loops – simple practical example

Well, the problem is that the variable i, within each of your anonymous functions, is bound to the same variable outside of the function.

ES6 solution: let

ECMAScript 6 (ES6) introduces new let and const keywords that are scoped differently than var-based variables. For example, in a loop with a let-based index, each iteration through the loop will have a new variable i with loop scope, so your code would work as you expect. There are many resources, but I'd recommend 2ality's block-scoping post as a great source of information.

for (let i = 0; i < 3; i++) {
funcs[i] = function() {
console.log("My value: " + i);
};
}

Beware, though, that IE9-IE11 and Edge prior to Edge 14 support let but get the above wrong (they don't create a new i each time, so all the functions above would log 3 like they would if we used var). Edge 14 finally gets it right.



ES5.1 solution: forEach

With the relatively widespread availability of the Array.prototype.forEach function (in 2015), it's worth noting that in those situations involving iteration primarily over an array of values, .forEach() provides a clean, natural way to get a distinct closure for every iteration. That is, assuming you've got some sort of array containing values (DOM references, objects, whatever), and the problem arises of setting up callbacks specific to each element, you can do this:

var someArray = [ /* whatever */ ];
// ...
someArray.forEach(function(arrayElement) {
// ... code code code for this one element
someAsynchronousFunction(arrayElement, function() {
arrayElement.doSomething();
});
});

The idea is that each invocation of the callback function used with the .forEach loop will be its own closure. The parameter passed in to that handler is the array element specific to that particular step of the iteration. If it's used in an asynchronous callback, it won't collide with any of the other callbacks established at other steps of the iteration.

If you happen to be working in jQuery, the $.each() function gives you a similar capability.



Classic solution: Closures

What you want to do is bind the variable within each function to a separate, unchanging value outside of the function:

var funcs = [];

function createfunc(i) {
return function() {
console.log("My value: " + i);
};
}

for (var i = 0; i < 3; i++) {
funcs[i] = createfunc(i);
}

for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}


Related Topics



Leave a reply



Submit