简体   繁体   中英

Confusion regarding javascript prototype explanation defined in Mozilla developer network

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

原型图

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM