What is a constructor function?
Constructors are used to assign values to initialized instantiated objects
Execution process
1. Create a new object by using the New keyword
2. The constructor assigns the newly created object to the This object within the function
3. Add members to new objects through the This keyword within a function
4. Return the newly created object by default
Precautions
1. Constructor name is best capitalized
2. Use with new
3. If you set the return value manually
A) return a basic type of data (including undefined and null), the function still returns the newly created object
b) Return an object of type data, the function will return this object
4. If you do not give constructor arguments when calling a function, the constructor will not create a new object, and this will point to the window, and the added attributes will be added to the window
What is a prototype?
When the constructor is created, the system creates a mysterious object associated with it by default, which is the prototype
The role of prototypes?
The properties and methods in the prototype can be shared by all objects instantiated by the constructor associated with it
Can expand built-in objects
How to get a prototype
constructor. prototype
Instantiates an object. __proto__
How prototypes are used
Direct Replacement prototypes
The prototype of the object created before replacing the prototype and the prototype of the object created after the substitution is not the same
Substitution is not a modification, it is a whole replacement.
Add members to the prototype using the object's dynamic properties (
(Member: A generic name for properties and methods)
. Cosntructor: The prototype function points to the constructor of the prototype
A) When a point syntax is assigned to a property, the prototype is not searched.
b) when assigning a value using Point syntax , if the property does not exist in the object , it will be added to the object and will not be found in the prototype
Review of the constructor function