Constructor: Another way to execute a function, create an object after execution, and create a prototype object.
Prototype chain: A pointer to an object access constructor.
Functions: Function object.
The object function: All ancestor objects that create objects are also implemented by Function objects.
Properties of the constructor:
1. When a constructor is declared, the syntax is exactly the same as the normal function,
2. The this pointer is different in the function body
3. Constructors are called differently than normal functions
4. Constructors are typically used to create a class that follows the code specification for C #, capitalized in the first letter.
Run the following code (and trace func):
<! DOCTYPE html>
Effect:
Type of Detection fun.prototype:
View Fun:
Test this with a normal function call (the program does not contain a reference to this, and the undefined appears):
Assign a value to this (it must be referenced in the program body):
Run the following code:
<! DOCTYPE html>
To run the code:
<! DOCTYPE html>
This is in the constructor:
The above is different for this in the normal function and the constructor:
1.this represents the current object in other languages and is very flexible in JS
2.this is related to the running environment of the current function
3. For normal functions, this represents the run environment object that invokes the current function in the function body
4. This represents the object created by the current constructor for the constructor function
5. In the global environment this represents the Global Environment (Window object)
Constructor: The new fun execution process:
1. Create an empty obj object
2. The prototype chain of an empty object (__proto__) points to the function object prototype object
3. Modify the function's this pointer to the newly created object reference.
4. Execute function body (normal function only executes fourth step)
Constructors in JavaScript