given MDN JS Inheritance article, we have these lines
My question is, why use Object.create and not just Person.prototype?
I understand the need to link prototypes.
But here is console example rendering the call to Object.create in fact not connecting the inherited methods:
Why is that? is it mistake in the article?
The problem with
Teacher.prototype = Person.prototype
is that then, there is no actual inheritence going on - both prototypes will reference the same object. If you proceed to add a function to Teacher's prototype, for examplegetClassTaught()
, that will mutatePerson.prototype
, which should not have that method.You also wouldn't be able to shadow
Person
functions without replacing them entirely. For example, if there's agreeting()
function onPerson.prototype
, and you assign anothergreeting()
function toTeacher.prototype
, you'll be overwriting the function onPerson.prototype
- otherperson
s callinggreeting()
may not work anymore, because the function is now Teacher-specific, rather than Person-generic.getOwnPropertyNames
only shows you the property names directly on the object itself - it does not show inherited property names. When you useObject.create(Person.prototype)
,greeting
is inherited from thePerson
prototype; it's not directly onTeacher.prototype
, so it doesn't show up ingetOwnPropertyNames
.