Created
October 6, 2015 21:40
-
-
Save gaand/c241371efa3f6f863fc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Verison 0 | |
'use strict'; | |
var counterFactory = function counterFactory() { | |
return function() {}; | |
}; | |
var firstCounter = counterFactory(); | |
// firstCounter contains a reference to | |
// a new instance of the minimal function | |
var secondCounter = counterFactory(); | |
// secondCounter contains a reference to | |
// a new instance of the minimal function | |
// it is a different object than the one | |
// stored in firstCounter | |
console.log('types match:', | |
typeof firstCounter === typeof secondCounter); | |
console.log('objects match:', | |
firstCounter === secondCounter); | |
// Version 1 | |
'use strict'; | |
var counterFactory = function counterFactory() { | |
return function() {}; | |
}; | |
var firstCounter = counterFactory(); | |
// firstCounter contains a reference to | |
// a new instance of the minimal function | |
var secondCounter = counterFactory(); | |
// secondCounter contains a reference to | |
// a new instance of the minimal function | |
// it is a different object than the one | |
// stored in firstCounter | |
console.log('types match:', | |
typeof firstCounter === typeof secondCounter); | |
console.log('objects match:', | |
firstCounter === secondCounter); | |
//Version 2 | |
'use strict'; | |
var counterFactory = function counterFactory() { | |
var counter = 1; | |
return function() { | |
return counter++; | |
}; | |
}; | |
var firstCounter = counterFactory(); | |
// firstCounter contains a reference to | |
// a new instance of a function that | |
// has access to a memory location | |
// it calls counter that contains the number 1 | |
var secondCounter = counterFactory(); | |
// secondCounter contains a reference to | |
// a new instance of a function that | |
// has access to a memory location | |
// it calls counter that contains the number 1 | |
// it is a different object than the one | |
// stored in firstCounter and what it refers to | |
// with the name counter is a different memory | |
// location than the one firstCounter accesses | |
console.log('firstCounter:', firstCounter()); | |
console.log('firstCounter:', firstCounter()); | |
console.log('firstCounter:', firstCounter()); | |
console.log('secondCounter:', secondCounter()); | |
console.log('secondCounter:', secondCounter()); | |
console.log('firstCounter:', firstCounter()); | |
//Version 3 | |
'use strict'; | |
var counterFactory = function counterFactory(initial) { | |
var counter = initial || 1; | |
return function() { | |
return counter++; | |
}; | |
}; | |
var firstCounter = counterFactory(); | |
// firstCounter contains a reference to | |
// a new instance of a function that | |
// has access to a memory location | |
// it calls counter that contains the number 1 | |
var secondCounter = counterFactory(100); | |
// secondCounter contains a reference to | |
// a new instance of a function that | |
// has access to a memory location | |
// it calls counter that contains the number 1 | |
// it is a different object than the one | |
// stored in firstCounter and what it refers to | |
// with the name counter is a different memory | |
// location than the one firstCounter accesses | |
console.log('firstCounter:', firstCounter()); | |
console.log('firstCounter:', firstCounter()); | |
console.log('firstCounter:', firstCounter()); | |
console.log('secondCounter:', secondCounter()); | |
console.log('secondCounter:', secondCounter()); | |
console.log('firstCounter:', firstCounter()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment