Skip to content

Instantly share code, notes, and snippets.

@ville6000
Last active November 3, 2015 10:25
Show Gist options
  • Save ville6000/a565fac5055797b7ae66 to your computer and use it in GitHub Desktop.
Save ville6000/a565fac5055797b7ae66 to your computer and use it in GitHub Desktop.
function Counter () {
var count = 0;
function logNewValue() {
console.log("Count is " + count);
}
return {
add: function () {
count += 1;
},
decrease: function () {
count -= 1;
},
value: function () {
logNewValue();
}
}
}
var c1 = new Counter();
var c2 = new Counter();
c1.add();
c1.add();
c1.add();
c1.decrease()
c1.value(); // Count is 2
console.log(c1.count); // undefined
console.log(typeof c1.logNewValue); // undefined
c2.add();
c2.decrease();
c2.decrease();
c2.value(); // Count is -1
console.log(c2.count); // undefined
console.log(typeof c2.logNewValue); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment