This article mainly introduces the _ proto _ attribute in JavaScript. For the so-called object in JavaScript, it points to the prototype of the object, if you need it, you can refer to JavaScript as an object-oriented speech, that is, everything is an object.
So how to generate an object? In the Java World, objects are produced by Class instances. In other words, objects are abstracted into a mold using this mold (Class) produce specific objects ).
However, JS does not have the concept of class. Some are "prototype", and objects are derived from prototypes. In general, in the Javascript world, "prototype" is not a mold, but a specific object ). All objects are derived from another object, and the derived object is the so-called "prototype object ".
There are three types of objects in javascript: one object created by the user, two constructor objects, and three prototype objects.
- The object created by the user is generally explicitly constructed using the new statement.
- The object of the constructor. A common constructor is a function that generates a common object by calling new.
- The prototype object that the constructor points.
Each class of these three objects has a property-_ proto _, which points to the prototype of the Object and can be traced back to the Object from any Object that follows it. prototype.
Constructor has a prototype object that points to a prototype object. when an object is created using this constructor, the _ proto _ attribute of the created object points to the prototype attribute of the constructor.
The prototype object has a constructor attribute pointing 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 take a look at _ proto _: some default methods.
The _ proto _ OBJECT also has a _ proto _ OBJECT. As we just mentioned, each object has a _ proto _ attribute pointing to its prototype object. Let's print the _ proto __in _ proto __:
console.log(obj.__proto__.__proto__); //--> null
The result is null, indicating that the top-level prototype object has been reached. Obj is defined by braces {}. The prototype object of obj is naturally the top-level object of JS.
Let's look at one end of the Code to enhance our understanding:
var parent = { name : "parent"};var child = { name : "child", __proto__ : parent};var subChild = { name : "subChild", __proto__ : child}console.log(subChild);
- SubChild. _ proto _ --> child
- Child. _ proto _ --> parent
- Parent. _ proto _ --> top-level prototype object