JS is an object-oriented language, and functions are objects. The following is a general introduction to the relationship between the prototype and the constructor.
Constructor mode
function Person (name,age) {
THIS.name = name;
This.age = age;
This.sayname = function () {
alert (this.name);
}
}
Define the constructor, capitalize the first letter of the function name, and instantiate the constructor with the new operator. The constructor itself is also a function, but it can be used to create objects. The features of the constructor are:
1. There is no explicit creation of an object
2. Assigning properties and methods to the Force this object
3. No Return statement
The implementation steps are:
1. Create a new object
2. Assign the scope of the constructor to the new object (this points to the new object)
3. Execute the code for the constructor, adding properties and methods to the constructor
4. Returning new objects
The main problem with constructors is that each method is created once on each instance.
Prototype mode
Each function has a prototype property, which is a pointer to its prototype object. The advantage of a prototype object is that it allows an object instance to share properties and methods in its prototype object.
The constructor property in the prototype object points to its constructor. The prototype of the constructor points to its prototype object. If you override the constructor with object literals, remember to specify the constructor property, otherwise constructor will point to object.
Instance
The new constructor creates an instance of the constructor, and the __proto__ in the instance object points to the prototype object corresponding to its constructor.
The prototype of the constructor points to its prototype object, the constructor of the prototype object points to its constructor, the constructor is instantiated with the New keyword, and the __proto__ of the instantiated object points to its original prototype object.
The effective way to create custom objects is to combine the constructor pattern with the prototype pattern. The constructor pattern defines the instance properties, the prototype schema definition methods, and the common properties. This allows each instance to have its own custom properties, while also sharing a reference to the method, saving the most cost of memory.
Related operators
Delete operator: removes the specified attribute from the instance. This allows access to properties of the same name in the prototype.
hasOwnProperty () method: detects that a property is present in an instance of the object, returns True if it belongs to an instance property, or False if it belongs to a prototype property.
In operator:
When used alone, returns true if this property exists in the object, regardless of whether the property is an instance property or a prototype property. Name in Object
When you use the for-in loop, all the properties that the object can enumerate are returned.
Each constructor has a prototype object that contains a pointer to the constructor (constructor), and the instance contains an internal pointer to the prototype object (__PRO__).
JS prototypes, examples, relationships between constructors