Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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
You can’t perform that action at this time.