/
Javascript Closure

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 content

Javascript Variables - Covering the core types
Javascript Variables - Covering the core types
More like this
Progress function for animation effects from first principles
Progress function for animation effects from first principles
Read with this
Javascript is a dynamic language
Javascript is a dynamic language
More like this
How do I change the content of a DOM element?
How do I change the content of a DOM element?
Read with this
Variables
More like this
The Javascript console and Developer Tools in Chrome
The Javascript console and Developer Tools in Chrome
Read with this