The internal process of new instantiation object in js

function Person () {
    
    
    this.name = name;
    this.age = age;
    this.job = job;
 
    this.sayName = function () {
    
    
        return this.name;
    };
}
 
var person = new Person("tom", 21, "WEB");
 
console.log(person.name);

Using the keyword new to create a new instance object goes through the following steps:

1. Create a new object, such as: var person = {};

2. The _proto_ property of the new object points to the prototype object prototype of the constructor.

3. Assign the scope of the constructor to the new object. (So ​​this object points to the new object)

4. Execute the code inside the constructor and add attributes to the this object in person.

5. Return the new object person.

Guess you like

Origin blog.csdn.net/qq_42535651/article/details/103336256