This article mainly introduces the process of a new object in js. It has good reference value. Let's take a look at it below. This article mainly introduces the process of a new object in js. It has good reference value. Let's take a look at it together with the small Editor.
Use the new Keyword to call a function (new ClassA (...)) Specific steps:
1. Create an empty object {}
2. Use the new object to call the function. this in the function is directed to the new instance object:
{}. Constructor ();
3. Set the constructor attribute of the new object to the name of the constructor, and set the _ proto _ attribute of the new object to point to the prototype object of the constructor.
4. Save the initialized object address to the variable on the left of the equal sign.
NOTE: If no return value is returned in the constructor or the return value is of the basic type (Number, String, Boolean), the new instance object is returned. If the return value is of the reference type, the actual return value is of this reference type.
Var foo = "bar"; function test () {this. foo = "foo";} new test (); // this in test indicates the new object and does not change the global foo Attribute console. log (this. foo); // "bar" console. log (new testThis (). foo); // "foo"; new and attribute access. the operator has the same priority and runs from left to right.
All of the above are personal understandings. If you have any mistakes, please leave a message to correct them.
For more articles about the process of adding a new object in js, please follow the PHP Chinese network!