The Introduction to Object-Oriented JavaScript is confusing me at one point.
They define a Person
class like this:
Properties should be set in the prototype property of the class (function) so that inheritance works correctly.
function Person(gender) {
this.gender = gender;
alert('Person instantiated');
}
Person.prototype.gender = '';
Later when they give an example of inheritance, they remove the gender
property (for clarity I assume), so I am not sure what the line Person.prototype.gender = '';
does in the first place.
I tried this:
function Person(gender) {
this.gender = gender;
}
Person.prototype.gender = 'default';
function Student(gender) {
Person.call(this, gender);
};
Student.prototype = new Person();
Student.prototype.constructor = Student;
var a = new Student('male');
var b = new Student();
console.log(a.gender); //prints 'male'
console.log(b.gender); //I expect 'default', but get undefined