preface
In work, many scenarios use the new operator to create an instance of a user-defined object type or of a built-in object with a constructor. Either direct {} or new constructor[([arguments])] essentially use the new operator to initialize instances.
Let’s review some of the basics of constructors. To set the stage for the next question.
The constructor
Constructors contain instance members and static members, each with the following meanings:
- Instance members: Instance members are members added inside the constructor via this.
- Static member: A member added to the constructor itself.
The difference between the two buddies:
- Instance members can only be accessed through the instantiated object.
- Static members can only be accessed through constructors
Let’s look at the following code.
function Father(name,age) {
// Instance member
this.name = name;
this.age = age;
}
// Static member
Father.sex = 'male';
let sun = new Father('private'.18);
console.log(sun); // Father {name: "Father ", age: 18}
console.log(sun.sex); // undefined cannot access the sex property
console.log(Father.age); // The undefined constructor cannot access instance members directly
console.log(Father.sex); // The male constructor accesses static members directly
Copy the code
So why can’t the created instance access the static members of the constructor? I’ll leave it there and we’ll answer it later.
The constructor creates the object
First, you create an object through the constructor.
function Father(name) {
this.name = name;
}
let son = new Father('Lisa');
console.log(son); //Father {name: "Lisa"}
Copy the code
At this point, son is a new object and an instance of creation.
Internal steps to create an object
The steps that occur internally during the process of new a new object:
- Create an empty object son
{}
; - Link to the prototype chain for SON
son.__proto__ = Father.prototype
; - Rebind this so that the constructor’s this points to the new object
Father.call(this)
; - Assign a value to a new object property (if necessary)
son.name
; - Returns a new object;
Manual implementation
Try implementing a new on your own:
function myNew(constructor, ... args){
const obj= {};
obj.__proto__ = constructor.prototype;
const ret = constructor.call(obj, ... args);
return ret instanceof Object ? ret : obj;
}
Copy the code
Application:
myNew(Father, 'Lisa') // output {name: 'Lisa'}
Copy the code
An instance of new cannot access the constructor’s static member resolution
Returning to the original question, why can’t an instance of new access the static members of the constructor? A look at the internal implementation of New makes this clear.
The key step in preventing an instance of new from accessing the static members of the constructor is that the instance does not inherit or cannot access the static members of the constructor.
In the internal implementation of New, the instance inherits only the stereotype of the constructor. Look at this step:
obj.__proto__ = constructor.prototype;
Copy the code
= = = = = = = = = = = = = = = = = = =
Ok, this is the summary of the relevant knowledge about the new operator, the end of the flower 🌸🌸🌸~.