The constructor

Meaning:

A constructor is a special function that initializes an object, assigning initial values to its member variables, and is used with new. You can extract some common properties and methods from objects and encapsulate them in this function

Four things new does when it executes

  1. Creates a new empty object in memory
  2. Let this point to the new object
  3. Execute the code inside the constructor to add properties and methods to the new object
  4. Return the new object (so there is no need for a return in the constructor

Disadvantages of constructors

When you create an object, the methods (functions are complex data types) in the object create space in the memory, that is, each instance of the object creates a space, there is a waste of memory.

Code sample
/* Constructor */
function Person(sName,sPhone){
	this.name=sName
	this.phone=sPhone
	this.run=function(){
	console.log('run'); }}// Instantiate to create a new prototype object that shares the properties and methods of the prototype
 const p1 = new Person("Zhang"."15623233232");
 const p2 = new Person("Bill"."17789899898");
			
console.log(p1.run===p2.run);//false indicates different addresses
Copy the code

New object instances created through constructors are independent of each other, and the methods added to the new object instance are owned only by that instance. Other instances do not have methods

/* Constructor */
function Person(sName,sPhone){
	this.name=sName
	this.phone=sPhone
	this.run=function(){
	console.log('run'); }}// Instantiate to create a new prototype object that shares the properties and methods of the prototype
const p1 = new Person("Zhang"."15623233232");
const p2 = new Person("Bill"."17789899898");
 // Add a method for the new object instance
// New object instances created through prototypes are independent of each other
p1.sayHello = function(){
  console.log("Hello?");
}
p1.sayHello() / / hello
p2.sayHello() / / an error
Copy the code

Constructor prototype (explicit prototype)

1. The function has prototype, which is an object that points to the current constructor reference address 2. All properties and methods of the Prototype object are owned by the constructor. 3. We can define the common method directly on the Prototype object, so that all object instances can share the method

Code sample
/* Constructor prototype*/
function Person(sName,sPhone){
	this.name=sName
	this.phone=sPhone
	this.run=function(){
	console.log('run');
 }
}
Person.prototype.run=function(){
 console.log('run');
}
 // Instantiate to create a new prototype object that shares the properties and methods of the prototype
const p1 = new Person("Zhang"."15623233232");
const p2 = new Person("Bill"."17789899898");
console.log(p1.run===p2.run);//true, which indicates the same address
Copy the code

Object prototype _proto_ (Implicit prototype)

1. Each object has a __proto__ attribute pointing to the constructor’s prototype object 2. 3.__proto__ object prototype is equivalent to prototype object prototype

Code sample
 console.log(p1.__proto__ === Person.prototype); //true
Copy the code

Prototype constructor

If we modify the prototype object and assign the constructor to an object, we must manually use the constructor to refer back to the original constructor. Because the new object overrides the prototype object of the original constructor

  function Person(sName,sPhone){
	this.name=sName
	this.phone=sPhone
	this.run=function(){
	console.log('run');
}
}
Person.prototype = {
  constructor: Person,
  run: function () {
   console.log("Run");
},
  eat: function () {
    console.log("吃东西"); }};// Instantiate to create a new prototype object that shares the properties and methods of the prototype
const p1 = new Person("Zhang"."15623233232");
const p2 = new Person("Bill"."17789899898");
console.log(Person.prototype);
console.log(p1.__proto__);
Copy the code
Lookup rules (prototype chain)

If it does, run is executed on the object. If it does not, __proto__ is executed on the prototype constructor. So you go up and up and you get a chain

A method that checks whether an object itself contains a property

1.hasOwnProperty

 let obj = {
    a: 5.b: 10};console.log("21", obj.hasOwnProperty("a"));
Copy the code

2 in

console.log('a' in obj)
Copy the code

What’s the difference between Prototype and Proto?

  1. Prototype is the constructor property
  2. __proto__ is a per-instance property that has access to the [[prototype]] property
  3. The instance’s __proto__ and its constructor’s prototype point to the same object