Javascript Closure

This is a special thing we can do with Javascript Functions.

A Javascript function can reference an external variable on the fly. Like this:

var NAMEfirst = "Fred"; setTimeout(function(){ console.log("Call " + NAMEfirst); }, 1000);

This is described as creating a “Closure”.

It is a very important technique in Javascript programming.

Lua also has closures as a core part of the language.

See this article pointing out trouble with closures in the context of a for loop.

See Wikipedia for more information on what a closure is.

 

Related pages