I am tring to emulate static members in javascript.
consider this code:
// option A for static members:
// constructor...
function Person(name) {
this.firstName=name;
}
Person.count=0; // like a static member...
var p=new Person("Dan");
Person.count++;
alert(Person.count); // 1;
and now this one:
// option B for static members:
// constructor..
function Person(name) {
this.firstName=name;
Person.prototype.count++;
}
Person.prototype.count=0; // like a static member...
var p=new Person("Dan");
alert(Person.prototype.count); // 1;
// ...
so, is it true to say that the main difference between those two, is that in the second example you can refer to the static member from the constuctor (due to the prototype decleration), while in the first example you can’t ?
In both examples you can get to it from within the constructor. The difference is in whether you can get to it through one of your objects.
Option A:
Option B: