This article is very short and pithy
Read ECMAScript@2021’s latest specification, the beginning of the interpretation of the relationship between prototype objects and objects, very good, the original text in a very easy to understand the way to explain the relationship between constructors, shared methods, prototype objects. So without further ado, the last picture
The realization in the figure is to show the prototype chain, and the dotted line is the implicit prototype chain. Through this figure, we can simply express the prototype chain that has been around in the past
- CF represents the constructor, whose prototype shows a pointer to CFp
- Cf1-cf5 are objects constructed from CF that share the CFp attribute CFP1
- Cf1-cf5 does not share CF attributes P1 P2
A one-sentence summary of the properties owned by the stereotype object bound by the stereotype chain that the instance object constructed by the constructor shares through the implicit stereotype chain constructor
Let’s take a closer look at this design and ask a few questions
- Why aren’t properties of constructors themselves shared directly with instance objects?
- What is an implicit prototype chain? What is an explicit prototype chain?
Why aren’t properties of constructors themselves shared directly with instance objects?
Considering that function is also an object in ECMAScript called functionObject, objects have properties, but for constructors, their own properties may be used for the construction process and do not want to be shared or exposed to the constructed instance, There is no such thing as a private property for functionObjects, so there is a stereotype object that distinguishes the properties of the constructor itself. If we look at it differently, does a constructor’s stereotype object with its own properties look like a class’s public and private members? Public is inherited, private is not. The result, at least, is the same.
What is an implicit prototype chain? What is an explicit prototype chain?
Prototype → prototype object → constructor; prototype → prototype object… The implicit prototype chain, on the other hand, is the instance → __ proto __ → prototype object constructed by the constructor
Figuring out these two issues solved some of my occasional confusion about the relationship between constructors, prototype objects, instances, and Prototype and __ proto __. As the spec ends, it’s not class-based, but provides a more flexible way to abstract common class methods
Refer to the link
- Tc39. Es/ecma262 / # se…