Last active
August 29, 2015 14:03
-
-
Save codyhanson/181a74ab1d7acb7e11c2 to your computer and use it in GitHub Desktop.
Javascript Scoping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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](); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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