my question is about creating objects which inherit from a "superclass".
I came up with a few different ways:
function PersonClass_1() {
this.age = 0;
this.gender = "female";
}
PersonClass_1.prototype.doAge = function () {
this.age++;
}
// create a new Object that inherits from Person.prototype
var p1 = Object.create(PersonClass_1.prototype);
// Call constructor function, with |this| set to the newly
// created object |p1|.
PersonClass_1.call(p1);
function PersonClass_2() {}
PersonClass_2.prototype.doAge = function () {
this.age++;
}
PersonClass_2.prototype.age = 0;
PersonClass_2.prototype.gender = "female";
// create a new Object that inherits from Person.prototype
var p2= Object.create(PersonClass_2.prototype);
// Call constructor function, with |this| set to the newly
// created object |p2|.
PersonClass_2.call(p2);
function PersonClass_3() {
this.age = 0;
this.gender = "female";
this.doAge = function () {
this.age++;
}
}
// create a new Object that inherits from Person.prototype
var p3 = Object.create(PersonClass_3.prototype);
// Call constructor function, with |this| set to the newly
// created object |p3|.
PersonClass_3.call(p3);
So what I end up with is three objects. The only difference seems to be the actual location of their attributes.
PersonClass_1 has the age and gender in the object itself, and the doAge in its prototype.
PersonClass_2 has the age, gender and doAge all in its prototype.
PersonClass_3 has the age, gender and doAge all in the object itself.
But the behaviour of the three Persons seems to be all the same. Or is their any major difference?