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.nameIs in thePerson.call(obj, 'life code ')The value is assigned later
  • Wait until theobjThe return is assigned tokenAfter that,ken.nameThat’s the code of life

So we can draw the following prototype chain:


  • ken.name: temporary variableobjnameobjThe return is assigned token.kenSome of the properties of the.
  • ken.hairColor:kenInstance objects first look for their ownhairColorIf not found, it will search along the prototype chain. In the above example, we only search inPersonObjecthairColor, is not defined on its prototype chain, so cannot be found.
  • ken.height:kenInstance objects first look for their ownheightIf it does not find it, it will search along the prototype chain. There is no prototype chain, so it cannot find it.
  • ken.say:kenIt looks for itself firstsayMethod is searched along the prototype chain if not found. In the above example, we find thePerson.prototypeDefined on thesaySo it’s on the prototype chainsayMethods.
  • In addition, insayMethod also accessthis.nameHere,thisRefers to its caller. ifkencallsay.kenIs the caller, therefore the outputken.nameThe 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 simpleJavaScriptObject (that is {});
  • Link this object (that is, set its constructor) to another object;
  • Take the object created in Step 1 as the objectthisContext;
  • Returns if the function does not return an objectthis.

Code implementation please refer to the following link:

https://github.com/sisterAn/JavaScript-Algorithms/issues/71

Copy the code