When creating a "child" object class in OOJS, I see two different ways of execution.
First,
function Person(name) {
this.name = name;
this.sayHi = function(){
alert("Hi");
}
}
function Teacher(name) {
Person.call(call, name);
}
}
var teacher1 = new Teacher("Tom");
Second,
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function() {
alert("Hi");
}
function Teacher(name) {
//same as the first example
}
Teacher.prototype = Object.create(Person.prototype);
Object.defineProperty(Teacher.prototype, 'constructor', {
value: Teacher,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true
});
The second method seems unnecessarily complicated. I am still able to access Teacher.prototype through both methods. What's the reasoning behind the second method?