Home >Web Front-end >JS Tutorial >Understanding JavaScript's prototype attribute_javascript tips
In fact, prototype can be summarized in just a few sentences:
Any prototype is an object, and only objects have prototypes
Only Function has the prototype attribute, which is the prototype inherited by the object generated when this Function is used as a constructor. The prototype of a Function has nothing to do with its prototype attribute
The prototype of an object can be accessed through the non-standard attribute __proto__ or the ECMAScript5 method Object.getPrototypeOf().
1 is actually wrong. Object, the object at the end of the prototype chain, has no prototype. But for the sake of simpler expression. After looking at the prototype chain, you will understand how undefined methods such as .toString() come from.
The ambiguity mentioned above is based on literal understanding, and there is no ambiguity in the grammar itself. Prototype means prototype, but the prototype of an object is not accessed by prototype.
Function has a prototype attribute, but it has nothing to do with its own prototype. After understanding this, it will be much easier to understand if you read articles about prototype chain and inheritance.
Here are some examples to deepen your understanding: