Home >Web Front-end >JS Tutorial >A brief introduction to the usage of new operator in js (code)
The content of this article is a brief introduction (code) about the usage of new operator in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
var Func=function(){}; var func=new Func ();
new has gone through four stages:
1. Create an empty object
varobj=new Object();
2. Set up the prototype chain
obj.__proto__= Func.prototype;
3. Let this in Func point to obj and execute the function body of Func.
var result =Func.call(obj);
4. Determine the return value type of Func:
If it is a value type, return obj. If it is a reference type, an object of this reference type is returned.
if (typeof(result) == "object"){ func=result; }else{ func=obj;; }
Related recommendations:
Detailed explanation of new operator in JavaScript
Examples of constructor and new operator in JavaScript Detailed explanation
The above is the detailed content of A brief introduction to the usage of new operator in js (code). For more information, please follow other related articles on the PHP Chinese website!