I wrote this code to teach myself about JavaScript closures:
function1 = function(){
var variable = \"foo\"
var function2 = function(argument){
conso
Whenever JavaScript executes the function3 function, a 'scope' object is created to hold the local variable named by you as a variable ("foo"). Note that your JavaScript code cannot directly access this scope object. And thus the value "foo" is available to the inner function, though the outer function has returned.
Does JavaScript traverse some kind of closure chain, similarly to how it traverses the prototype chain?
Yes. "Scope objects form a chain called the scope chain, similar to the prototype chain used by JavaScript's object system.
A closure is the combination of a function and the scope object in which it was created. Closures let you save state — as such, they can often be used in place of objects"
Read more here: