Someone asked me what happened to Teacher Ma, I said what happened, send me two screenshots
I have a look, yuan Lai is left day, there are two lines of code, give me the whole meng……
[[prototype]] and prototype
The former is the Internal slot shared by all objects (as defined in the ES6 documentation);
The latter is an attribute (common attribute) of a function that points to an object by reference and holds properties and methods shared by all instances (e.g. Array.sort method).
In ECMAScript 2020 Language Specification 9.1, it is defined as follows
All ordinary objects have an internal slot called [[Prototype]]. The value of this internal slot is either null or an object and is used for implementing inheritance. Data properties of the [[Prototype]] object are inherited (and visible as properties of the child object) for the purposes of get access, but not for set access. Accessor properties are inherited for both get access and set access.
[[prototype]] [[prototype]] [[prototype]]
When JS is designed, the new operator is followed by a constructor, so new Foo() calls constructor.
So [[prototype]] refers to the constructor. Each Object has its own constructor, and you can find it in a chain of constructors. At the top of the chain is Object, and Object’s prototype is null.
In the specification, [[prototype]] is called Internal slot1. All objects have it. In JavaScript everything is an object, so it’s everywhere.
For example, every object has a prototype. [[prototype]] is used to describe this behavior of objects. It is not open to users (in the early days of language development), so I call it pseudo-attribute 2.
You say again, I print out the object, there is no [[prototype]], young people do not speak martial virtue, to cheat!
[[prototype]] doesn’t work in the browser, __proto__ works in the browser. [[prototype]] is an Internal Slot, and __proto__ is an Internal Slot.
Translation: The early JavaScript language didn’t allow users to access prototypes, so browsers figured out a way to make _proto_ to access prototypes; In addition, it is an accessor property. It is also described in Appendix B of the specification 4.
In the new specification, two new methods object.getPrototypeof () and Object.setPrototypeof () are provided and recommended instead of __proto__.
Prototype, on the other hand, is similar to static properties/methods. We want all instances of an object to share some methods/properties to save resources. It says so in specification 4.3.1
Each constructor is a function that has a property named “prototype” that is used to implement prototype-based inheritance and shared properties.
So, going back to the beginning, the last two lines of code in this picture will give you a sense of what it means. One last question, whyObjectSetPrototypeOf()
no.
, node. js source code says:
Ruan Yifeng teacher of this article is too good, I write to see the words that do not understand, suggest to see this.
OK, just this.