Created
April 23, 2014 20:12
-
-
Save jhamon/11230597 to your computer and use it in GitHub Desktop.
Another way to achieve private class variables using closure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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