The principle of new operators

About the principle of the new operator:

Share a picture

1. Explanation on the Red Book:

(1) Create a new object

(2) Set the function Assign the scope of the new object to the new object

(3) Execute the code in the constructor

(4) Return the new object

2. Explanation on MDN:

(1) A new object inherited from Foo.prototype is created

(2) The constructor Foo is called with the specified parameters, and this Bind to the newly created object. new Foo is equivalent to new Foo(), that is, when no parameters are specified, Foo is called without any parameters

(3) If the constructor returns an “object”, then this object will replace the entire The result of new. If the constructor does not return an object, the result of new is the object created in step (1)

3. Summary:

When using the new operator , Actually did these three things:

// Created an empty object obj, and pointed the __proto__ member of this empty object to the Foo function object prototype member object

var obj = {};
obj.__proto__
= Foo.prototype;

// Replace the this pointer of the Foo function object with obj, and then Then call the Foo function
Foo.call(obj);// Determine whether the instance type is an object, if yes, return the instance, otherwise return the constructor
if (typeof obj ==='object') {
return obj;
}
else {
return Foo;
}

In other words, the following changes have occurred inside the function:

(1) Create a new object, and The prototype object Foo.prototype that inherits the constructor Foo

(2) The constructor Foo is executed, the corresponding parameters are passed in during execution, and this is designated as the new instance

( 3) Judge whether the instance is an object, if yes, return the instance, otherwise return the constructor

//< /span> creates an empty object obj, and points the __proto__ member of this empty object to the Foo function object prototype member object

var obj = {};
obj.__proto__
= Foo.prototype;

// Replace the this pointer of the Foo function object with obj, and then Then call the Foo function
Foo.call(obj);// Determine whether the instance type is an object, if yes, return the instance, otherwise return the constructor
if (typeof obj ==='object') {
return obj;
}
else {
return Foo;
}

(1) Create a new object and inherit the prototype object Foo.prototype of the constructor Foo

( 2) The constructor Foo is executed, and the corresponding parameters are passed in during execution, and this is designated as the new instance.

(3) Determine whether the instance is an object, if it is, then return the instance, otherwise return the constructor< /p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2779 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.