While reading through https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance I saw that we should replace the Teacher
's prototype with a new one, which has its prototype set to Person
:
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 });
I'd rather do it this way, it seems more logical to me:
Teacher.prototype.__proto__ = Person.prototype
As far as I understand, it ends up with the same result and the constructor stays correct, I do not have to replace it.
Is there any difference between MDN's approach and mine?
//EDIT
As @ Asutosh pointed out, MDN discourages from using __proto__
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto). However, MDN also says to use Object.setPrototypeOf
instead. So, can I do this:
Object.setPrototypeOf(Teacher, Person.prototype)
instead of original
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 });