English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The specific steps to use the new keyword to call a function (new ClassA(...)) are as follows:
1. Create an empty object {}
2. Use the new object to call the function, and the this in the function points to the new instance object:
{}.constructor();
3. The constructor property of the new object is set to the name of the constructor, and the __proto__ property of the new object points to the prototype object of the constructor
4. The address of the newly initialized object is saved to the variable on the left side of the equal sign
Note: If the constructor does not return a value or returns a value of a basic type (Number, String, Boolean), then the returned value is a new instance object; if the returned value is a value of a reference type, then the actual returned value is this reference type.
var foo = "bar"; function test () { this.foo = "foo"; } new test(); //In the test, this refers to the new object and does not change the global foo property console.log(this.foo); // "bar" console.log(new testThis().foo); // "foo"; the new and property access operators have the same precedence and are executed from left to right
The above are personal understandings, and welcome to leave a message to correct any errors.
I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for Yell Tutorial!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)