0

I have read about javascript prototype and read some stackoverflow question as well like how-does-javascript-prototype-work and I was finally understanding prototype properly but then as I was going through documentation on Mozilla Developer Netork (MDN) regarding prototype under this Details_of_the_Object_Model MDN

Prototype diagram

Under that there is a note

Note: Directly assigning to FunctionName.prototype removes its original prototype's "constructor" property. As a result, (new WorkerBee).constructor yields "Employee" (instead of expected "WorkerBee"). Care must be taken to preserve the original prototype's constructor. For instance, assign the parent to FunctionName.prototype.__proto__ instead. For example, WorkerBee.prototype.__proto__ = new Employee; This way, (new WorkerBee).constructor yields expected "WorkerBee".

I can't seem to understand what this statement mean

Directly assigning to FunctionName.prototype removes its original prototype's "constructor" property.

Does it mean that Manager.prototype = new Employee will replace Manager.__proto__ = Function.prototype to Employee.prototype?

As a result, (new WorkerBee).constructor yields "Employee" (instead of expected "WorkerBee")

What does it mean that it will yield Employee? I know we should not directly use __proto__ but the above statement specify to use FunctionName.prototype.__proto__. In which case this is true?

Can someone explain with example what does it try to convey?

Community
  • 1
  • 1
Raunak Kathuria
  • 3,185
  • 1
  • 18
  • 27
  • In reply to your comment; yes that article has room for improvement. Either look at the code in the link of my answer or here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – HMR Oct 16 '14 at 13:07

1 Answers1

1

Prototype on functions are used as proto for objects generated by these functions.

A functions proto points to Function.prototype since a function is also an object instance by itself. Setting it's proto does not affect it's prototype but re setting it's prototype does affect the prototype. constructor

You should not set Child.prototype to an instance of Parent, use Object.create instead.

What prototype.constructor is and more is explained here: Prototypical inheritance - writing up

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160
  • So what you are saying is that instead of Manager.prototype = new Employee we should use var Manager = Object.create(Employee)? and then adding properties specific to Manager? Is MDN document outdated? – Raunak Kathuria Oct 16 '14 at 08:07