On this link below:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance#Setting_Teacher()'s_prototype_and_constructor_reference
I don’t quite understand what point 2 and point 3 meant. Why do we need to write Teacher.prototype.constructor = Teacher;
and what “problem” can it cause if we don’t?
So when you run the line
Teacher.prototype = Object.create(Person.prototype)
You are setting the prototype of the Teacher
object equal to Person.prototype
. This is OK, but seen as Person.prototype.constructor
is equal to Person
, Teacher.prototype.constructor
is now also equal to Person
. This means that if you try to do something like
let myNewTeacher = new Teacher();
You’ll actually be constructing a new instance of Person
, not Teacher
. This is obviously wrong, so to fix this and make new Teacher()
work correctly, you need to run
Teacher.prototype.constructor = Teacher;
Does that help?