SoFunction
Updated on 2025-04-10

In-depth exploration of javascript prototype is not as simple as prototype inheritance


Note that the above example shows that using prototype can quickly create multiple copies of objects. Generally speaking, using prototype to create large quantities of complex objects is much faster than using any other method to copy objects. Note that using an object as a prototype to create a large number of new objects is the essence of prototype pattern.
Here is an example:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

In addition to the techniques mentioned above, prototype is used because of its unique characteristics and other uses, which may be used as the most widely and widely known to simulate inheritance. This is left to be discussed in the next section.

3 The essence of prototype

The above has already talked about the role of prototype. Now let’s reveal the essence of prototype through the rules.
We say that the behavior of prototype is similar to the static domain in C++, adding a property as a property of prototype, which will be shared by all instances created by the type, but this sharing is read-only. In any instance, you can only override this property with your own attribute of the same name, and you cannot change it. In other words, when an object reads a certain attribute, it always checks the attribute table of its own domain first. If there is this attribute, it will return this attribute. Otherwise, it will read the prototype domain and return the attribute on the protoype domain. In addition, JavaScript allows the protoype domain to refer to any type of object. Therefore, if the read of the protoype domain still does not find this property, JavaScript will recursively search for the prototype domain of the object pointed to by the prototype domain until the prototype domain of the object is itself or a loop occurs. We can use the following figure to describe the relationship between the prototype and the object instance:
        //TODO:

4 The value and limitations of prototype

From the above analysis, we understand prototype. Through it, it can safely create a large number of instances based on an object. This is the true meaning of prototype and its value lies. Later we will see that using the feature of prototype, it can be used to simulate object inheritance. However, you should know that although prototype is also an important value of its use, it is definitely not its core. In other words, the reason why JavaScript supports prototype is definitely not just used to implement its object inheritance. Even without prototype inheritance, JavaScript's prototype mechanism is still very useful.
Since prototype only builds copy of the type based on objects, it also has great limitations. First, it does not appear as a copy of the value on the prototype field of the type, but a copy of the reference, which brings "side effects". Changing the attribute value of the attribute of a reference type on a certain prototype (a quite difficult explanation: P) will completely affect every instance of this type creation. Sometimes this is exactly what we need (such as changing the default value of all objects in a certain class), but sometimes this is what we don't want (such as when class inheritance). Here is an example:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]