Skip to content

Instantly share code, notes, and snippets.

@ChillyBwoy
Last active January 9, 2018 06:05
Show Gist options
  • Save ChillyBwoy/1f682e9c1902269c6b77e311ff823492 to your computer and use it in GitHub Desktop.
Save ChillyBwoy/1f682e9c1902269c6b77e311ff823492 to your computer and use it in GitHub Desktop.
function factory() {
var count = 0;
return function() {
function fx() {
count += 1;
return fx;
}
fx.valueOf = function() {
return count;
};
return fx();
}
}
var f1 = factory();
console.log(f1()()); // 2
console.log(f1()()); // 4
console.log(f1()()); // 6
var f2 = factory();
console.log(f2()); // 1
console.log(f2()()); // 3
console.log(f2()()()); // 6
var f3 = factory();
console.log(f3()()()); // 3
console.log(f3()()()); // 6
console.log(f3()()()); // 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment