-
-
Save iam-peekay/aa40104bc1c645fa4d7f9f772057af77 to your computer and use it in GitHub Desktop.
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
function iCantThinkOfAName(num, obj) { | |
// This array variable, along with the 2 parameters passed in, | |
// are 'captured' by the nested function 'doSomething' | |
var array = [1, 2, 3]; | |
function doSomething(i) { | |
num += i; | |
array.push(num); | |
console.log('num: ' + num); | |
console.log('array: ' + array); | |
console.log('obj.value: ' + obj.value); | |
} | |
return doSomething; | |
} | |
var referenceObject = { value: 10 }; | |
var foo = iCantThinkOfAName(2, referenceObject); // closure #1 | |
var bar = iCantThinkOfAName(6, referenceObject); // closure #2 | |
foo(2); | |
/* | |
num: 4 | |
array: 1,2,3,4 | |
obj.value: 10 | |
*/ | |
bar(2); | |
/* | |
num: 8 | |
array: 1,2,3,8 | |
obj.value: 10 | |
*/ | |
referenceObject.value++; | |
foo(4); | |
/* | |
num: 8 | |
array: 1,2,3,4,8 | |
obj.value: 11 | |
*/ | |
bar(4); | |
/* | |
num: 12 | |
array: 1,2,3,8,12 | |
obj.value: 11 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment