Skip to content

Instantly share code, notes, and snippets.

@gaand
Created October 6, 2015 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gaand/c241371efa3f6f863fc7 to your computer and use it in GitHub Desktop.
Save gaand/c241371efa3f6f863fc7 to your computer and use it in GitHub Desktop.
//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