The constructor is the method of the object name
Function Phone(width,height,brand){this.width =width; // Member attribute this.height =height; this.brand =brand; This. Call =function(){console.log(" call ")}}Copy the code
The point of a prototype object is that you don’t have to open up the memory space of a member method every time a new object is created
All instances have access to the methods of the prototype object at the same address
Prototype call = function(){console.log()} var xm = new Phone(100,100,'xm') var hw = new Phone (120120, 'hw) xm. Call () call () hw.Copy the code
Prototype object: It is an object. The js system had this class when it was created
- Each stereotype object has a constructor pointing to the constructor method
- The proto of the instance is Phone. Prototype exactly the same as the prototype object pointing to that object
- Phone on the prototype. The prototype. The default constructor property is pointing to the constructor of the Phone
- So! [example].proto.constructor is the constructor that calls Phone. Prototype.
- It’s still a prototype object, but the instance can be accessed by the __proto__ keyword
- Constructor just tells you what the object’s constructor is
The console. The log (xm) __proto__) constructor) console. The log (Phone) prototype) constructor) a trifle xm __proto = = = Phone. The prototype / / trueCopy the code
If you overwrite the prototype chain with your own defined object, then constructor is lost and needs to be specified manually
Prototype = {constructor: call:function(){console.log()}}Copy the code
Or when you inherit, if you don’t want to use the constructor of a subclass, use the parent class, and specify that manually as well
Prototype chain
Why prototype chains? Because the prototype object is also an object, as long as the object, as said above, haveproto
- When an instance accesses a method, it first looks to see if the object itself has a method, and if not, looks to its prototype, such as the call call method
- If there are no more, find Object all the way to the prototype Object’s -> prototype Object
- Proto == null Object. Prototype