Skip to content

Instantly share code, notes, and snippets.

@codyhanson
Last active August 29, 2015 14:03
Show Gist options
  • Save codyhanson/181a74ab1d7acb7e11c2 to your computer and use it in GitHub Desktop.
Save codyhanson/181a74ab1d7acb7e11c2 to your computer and use it in GitHub Desktop.
Javascript Scoping
#! /usr/bin/env node
function gen(val){
return function(){
console.log('My value is:' + val);
}
}
var a = [1, 2, 3, 4, 5];
var fps = [];
for (var i = 0; i < a.length; i++){
var value = a[i]
fps.push(gen(value));
console.log('Pushed function to print out value:' + a[i]);
}
for (var j = 0; j < fps.length; j++){
console.log('Invoking function number:' + j);
fps[j]();
}
#! /usr/bin/env node
var a = [1, 2, 3, 4, 5];
var fps = [];
for (var i = 0; i < a.length; i++){
var value = a[i]
fps.push(function(){
//console.log('My value is:' + a[i]);
console.log('My value is:' + value);
});
console.log('Pushed function to print out value:' + a[i]);
}
for (var j = 0; j < fps.length; j++){
console.log('Invoking function number:' + j);
fps[j]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment