The front little white mutters.

If there is anything wrong, please advise me.

preface

Prototype (__proto__) constructor (constructor)

In MDN, it is explicitly stated that functions are also objects.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

We know that functions have prototypes and objects have constructors.

So, when a function exists as an object, it also has a constructor.

Therefore, functions are both functions and objects. Constructor; prototype

1. Two sides of a function

The constructor of a function

The constructor property of a custom Function pointing to the Function Function

Function Hello() {this.a = 1} console.log(hello.constructor) // The constructor of the custom function points to the function console.log(hello.constructor) === Function) console.log(hello.prototype) // The Hello Function points to the default prototype because it did not rewrite the prototypeCopy the code

1.2 Function prototype

The prototype of a function is an object.

Without overriding the function prototype, the default function prototype has the Contructor property, which points to the function itself.

function Hello() {
    this.a = 1
    b() {}
}
console.log(Hello.prototype.constructor === Hello) //true
Copy the code

Rewriting a function prototype with a direct assignment causes the Contructor property in the prototype to be discarded.

To establish the connection between Prototype and the original function, you need to manually assign values.

function Hello() { } console.log(Hello.prototype.contructor === Hello) //true Hello.prototype = { greeting () {} } The console. The log (Hello. Prototype. Its constructor = = = Hello) / / false / / need to manually assign its constructor attributes to establish the connection between the prototype and the function Hello.prototype.constructor = Hello console.log(Hello.prototype.contructor === Hello) //trueCopy the code

Don’t paste this code, do it yourself. Do not know why, paste is not good, who can tell me this is why?

Therefore, in general, the constructor of a function prototype refers to the function itself.

2. The object

2.1 对象的构造函数constructor

Normally, the constructor of an object points to the function itself.

Same thing with the new object.

var obj = {} var arr = [] function Hello() { this.a = 1 this.b = 2 } var hello = new Hello() console.log(obj.constructor  === Object) //true console.log(arr.constructor === Array) //true console.log(hello.constructor === Hello) //trueCopy the code

Therefore, the prototype of the object’s constructor points to the prototype of the function that created the object.

function Hello() {
}
var hello = new Hello()
console.log(hello.constructor.prototype === Hello.prototype) //true
Copy the code

2.2 __proto__ of objects

The __proto__ attribute is found when printing an object from a custom function new.

Object’s __proto__ property, pointing to the prototype of the object’s constructor.

function Hello() {
    this.a = 1
    this.b = function(){}
}
var hello = new Hello()
console.log(hello.__proto__ === hello.constructor.prototype) //true
console.log(hello.__proto__ === Hello.prototype)
Copy the code

3. Prototype, __proto__, constructor correlation summary

3.1 the prototype and the constructor

  1. Function prototype, whose constructor property, by default, points to the function itself;
  2. The constructor of an object new points to the function itself by default;
  3. The constructor prototype of the object derived from new, which by default points to that function’s prototype;
  4. If you rewrite the prototype of a function (which is assigned directly), you break the link between the prototype and the original function. So none of the above is going to be true. To re-establish the connection, you must manually add the constructor attribute to the function prototype, whose value is specified as the original function.

3.2 __proto__ and prototype

Regular objects have a __proto__ attribute, which by default points to the function’s prototype, which is the prototype of the object’s constructor.

4. Develop

Object.create()

The object.create () method creates a new Object, using an existing Object to provide the __proto__ of the newly created Object.

The Object.created() method creates a new object, using an existing object as the prototype of the newly created object.

Therefore, when we create a new Object with object.create (), we change the __proto__ reference of the regular Object.

var here = {
    a: 1,
    b() {}
}
let copy = {}
console.log(copy.__proto__ === copy.constructor.prototype) //true
copy = Object.create(here)
console.log(copy.__proto__ === copy.constructor.prototype) //false
Copy the code