Object is new, so what’s going on?
What, startled by the headline, is that programmers have objects? Of course I do. I don’t know what year it is. When you meet someone, they ask you what happened?
Tell it professionally:
New is used to instantiate a class and then allocate an instance object in memory.
Let’s start with an example:
function Person(name) {
this.name = name;
}
Person.hairColor = "black";
Person.prototype.say = function() {
console.log("My name is " + this.name);
};
var ken = new Person("Life code");
console.log(
ken.name, // "life code ",
ken.hairColor, // undefined
ken.height // undefined
);
ken.say(); // "My name is My code"
console.log(
Person.name, // "Person"
Person.hairColor // "black"
);
Person.say(); // Person.say is not a function
Copy the code
What happens to this code? Here’s the answer
The strict
Line 8 is the key:
var ken = new Person("Life code");
Copy the code
Person is an ordinary function, except that if you add new to it, it becomes a constructor.
So when the JS engine parses the code, it will do a lot of internal processing, pseudo code is as follows:
new Person('Life Code') = {
var obj = {}; // Define objects
var proto = Object.create(Person.prototype); // Copy the prototype
obj.__proto__ = proto; // Create a prototype chain
// obj->Person.prototype->Object.protorype->null
var res = Person.call(obj, 'Life Code'); // equivalent to obj.Person(' life code ')
// If no value is returned or a non-object value is returned, obj is returned as the new object:
return typeof res === 'object' ? result || obj : obj;
}
Copy the code
We can conclude the following:
obj.name
Is in thePerson.call(obj, 'life code ')
The value is assigned later- Wait until the
obj
The return is assigned token
After that,ken.name
That’s the code of life
So we can draw the following prototype chain:
ken.name
: temporary variableobj
有name
,obj
The return is assigned token
.ken
Some of the properties of the.ken.hairColor
:ken
Instance objects first look for their ownhairColor
If not found, it will search along the prototype chain. In the above example, we only search inPerson
ObjecthairColor
, is not defined on its prototype chain, so cannot be found.ken.height
:ken
Instance objects first look for their ownheight
If it does not find it, it will search along the prototype chain. There is no prototype chain, so it cannot find it.ken.say
:ken
It looks for itself firstsay
Method is searched along the prototype chain if not found. In the above example, we find thePerson.prototype
Defined on thesay
So it’s on the prototype chainsay
Methods.- In addition, in
say
Method also accessthis.name
Here,this
Refers to its caller. ifken
callsay
.ken
Is the caller, therefore the outputken.name
The value of the.
conclusion
The new operator creates an instance of a user-defined object type or of a built-in object with a constructor. The new keyword performs the following operations:
- Create an empty simple
JavaScript
Object (that is {}); - Link this object (that is, set its constructor) to another object;
- Take the object created in Step 1 as the object
this
Context; - Returns if the function does not return an object
this
.
Code implementation please refer to the following link:
https://github.com/sisterAn/JavaScript-Algorithms/issues/71
Copy the code