This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Constructor problem

The constructor method is nice, but it can be a waste of memory.

Constructor prototype

Constructors The functions assigned by the stereotype are shared by all objects.

JavaScript states that each constructor has a Prototype property that points to another object. Note that prototype is an object whose properties and methods are all owned by the constructor.

We can define those immutable methods directly on a Prototype object so that all object instances can share them.

function Star(uname, age) {
    this.uname = uname;
    this.age = age;
}
Star.prototype.sing = function() {
	console.log('I can sing');
}
var ldh = new Star('Andy Lau'.18);
var zxy = new Star('Jacky Cheung'.19);
ldh.sing();// I can sing
zxy.sing();// I can sing
Copy the code

The object prototype

All objects have a __proto__ attribute that points to the prototype object constructor. We can use the constructor's properties and methods because the object has a __proto__ prototype. __proto__ object prototype is the same as the prototype object prototype. The point of __proto__ object prototype is to provide a direction, or a route, for the object search mechanism, but it is a nonstandard property and therefore cannot be used in actual development. It just points internally to the prototype object prototypeCopy the code

Constructor constructor

Both __proto__ and prototype objects have a constructor property inside. Constructor we call it a constructor because it refers back to the constructor itself. Constructor is used primarily to record which constructor the object refers to, and it can redirect the prototype object to the original constructor. In general, the methods of the object are set in the prototype object of the constructor. If we have methods for more than one object, we can assign to the prototype object as an object, but this overwrites the original contents of the constructor prototype object so that the modified constructor object no longer points to the current constructor. At this point, we can add a constructor pointing to the original constructor in the modified prototype object.Copy the code

If we modify the original stereotype object and assign the stereotype object to an object, we must manually use constructor to refer back to the original constructor as follows:

 function Star(uname, age) {
     this.uname = uname;
     this.age = age;
 }
 In many cases, we need to manually refer back to the original constructor using the constructor property
 Star.prototype = {
 // If we modify the original stereotype object and assign the stereotype object to an object, we must manually use constructor to refer back to the original constructor
   constructor: Star, // Manual setting refers back to the original constructor
   sing: function() {
     console.log('I can sing');
   },
   movie: function() {
     console.log('I'll be in a movie.'); }}var zxy = new Star('Jacky Cheung'.19);
console.log(zxy)
Copy the code

As a result of the above code running, set the constructor property as shown below:

If the constructor property is not set, as shown:

Prototype chain

Each instance object in turn has a __proto__ attribute, pointing to the constructor’s prototype object, which is also an object and has a __proto__ attribute, so that layer by layer the prototype chain is formed.

[img-yc821JFT-1597294444598] (images/img5.png)

1.8 Constructor instance and prototype object triangulation

1.The constructor's Prototype property points to the constructor prototype object2.The instance object is created by the constructor, and the __proto__ attribute of the instance object points to the constructor's prototype object3.Constructor of the prototype objectconstructorProperty refers to the constructor, the prototype of the instance objectconstructorProperty also points to the constructorCopy the code

Search mechanism for prototype chains and members

Any object has a prototype object, that is, the prototype property. Any prototype object is also an object, and that object has a __proto__ property.

When accessing properties (including methods) of an object, you first look up whether the object itself has the properties. If not, look for its prototype (that is, the prototype object __proto__ points to). If not, find the prototype of the prototype Object (Object's prototype Object). And so on until Object is found (null). The point of __proto__ object archetypes is to provide a direction, or a route, for the object member lookup mechanism.Copy the code