- or download this
var x = "hello "
var my_closure = function (y) {
alert(x + y);
}
my_closure("world");
- or download this
for (@some_array) {
# Declaring this in the loop creates a new variable per
...
};
}
# We now have one closure per array element.
- or download this
for (var i = 0; i < some_array.length; i++) {
// Declaring this in the loop creates a one variable for
...
});
}
// All closures now point at the last element.
- or download this
for (var i = 0; i < some_array.length; i++) {
// This declares and immediately calls a function, and x
...
)(some_array[i]); // And here is where we bind the value.
}
// We now have one closure per array element.