JavaScript is an object-oriented voice, that is, everything is an object.
So how to generate an object? In the Java world, objects are created by Class instances. In layman's terms, they abstract things into a mold and use this mold (class) to produce specific real objects (objects).
However, there is no concept of class in JS. Some are "prototypes", and objects are derived from prototypes. In layman's terms, in the world of JS, "prototype" is not a mold, but a concrete object (object). All objects are derived from another object, and this derived object is the so-called "prototype object".
There are three types of objects in javascript, 1 user-created object, 2 constructor object, 3 prototype object
Each of these three objects has a property -_proto__ attribute, which points to the prototype of the object, and can be traced back to Object.prototype from the point where any object follows it.
The constructor has a prototype object, which points to a prototype object. When the object is created through this constructor, the __proto__ attribute of the created object will point to the prototype attribute of the constructor.
The prototype object has a constructor property that points to its corresponding constructor.
Talk is cheap, show me the code! Let's take a look at the code:
var obj = {};console.log(obj);Let's expand __proto__ to see: are some default methods.
You will definitely happen that there is also a __proto__ object in this __proto__ object. As we just said, each object has a __proto__ attribute pointing to its prototype object. Let's print the __proto__ in this __proto__:
console.log(obj.__proto__.__proto__); //--> null
The result is null, indicating that the top-level prototype object has arrived. obj is defined in braces {}, and obj's prototype object is naturally the top-level object of JS.
Let's look at the code on one side to strengthen our understanding:
var parent = { name : "parent"};var child = { name : "child", __proto__ : parent};var subChild = { name : "subChild", __proto__ : child}console.log(subChild);