Skip to content

Instantly share code, notes, and snippets.

@jhamon
Created April 23, 2014 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhamon/11230597 to your computer and use it in GitHub Desktop.
Save jhamon/11230597 to your computer and use it in GitHub Desktop.
Another way to achieve private class variables using closure.
var Cat = (function () {
var numberOfCats = 0;
var Cat = function (name) {
this.name = name;
numberOfCats += 1;
}
Cat.prototype.numberOfCats = function () {
return numberOfCats;
};
return Cat;
}())
cat = new Cat('fluffy');
cat.numberOfCats(); // 1
cat2 = new Cat('garfield')
cat.numberOfCats(); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment