Prototype chain

There are several ways to create objects:

  • Create it literally:
var o1 = { name: 'song'}; literal creation will call the following new Object() by default to create an object
var o2 = new Object({ name: 'song'});
  • Created by constructor:
var Person = function(){ name: 'song'};
var o3 = new Person ();

  • Created by the Object.create method:
var Person1 = { name: 'song'};
var o4 = Object.create(Person1);

Prototype chain class: contains instance objects, constructors, prototype objects, and prototype chains

    Constructor: Any function that uses the new key to create an instantiated object is called a constructor.

    Instance Object: An object created using the new keyword is called an instance object.

Any function has a prototype property, which points to the prototype object of the function.

The constructor property of the prototype object points to the constructor of this function.

The __proto__ property of each instance object points to its prototype object.

    Prototype object: The prototype property of the constructor is the prototype object of the function.

    Prototype chain: The __proto__ property of an instance object points to its prototype object, and the __proto__ property of this prototype object points to the prototype object of this prototype object, and so on up and finally points to Object.prototype . A chain of multiple objects formed in this way is called a prototype chain.

The function of the prototype chain: the properties and methods on the prototype object are shared by its own corresponding instance objects. When an instance object calls one of its methods, the js engine will look for this instance, if not, it will look for the prototype object of this instance, if not, it will continue to look up along the prototype chain until it is found.

instanceof: o1 instanceof(M) Determines whether this object is an instance object of this constructor. In fact, it determines whether the __proto__ property of this object points to the same prototype object as the prototype property of this constructor.

new operator: what the new operator does.

  1. Create an empty object and inherit the prototype object of the constructor;
  2. When executing the constructor, the corresponding parameters will be passed in, and the context (this) will point to the new instance;
  3. Return this object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325590942&siteId=291194637