I want to keep track of how many objects have been created using my constructor function. Currently I'm getting the number by placing a humanCount
variable in the global namespace. But, it doesn't look good to me. My code is:
var humanCount = 0;
function Human(name, age){
this.name = name;
this.age = age;
humanCount++;
};
var john = new Human("JOHN", 35);
console.log(humanCount); // prints 1
var jane = new Human("JANE", 28);
console.log(humanCount); // prints 2
Any other way to achieve the functionality?