Home >Web Front-end >JS Tutorial >A brief introduction to the usage of new operator in js (code)

A brief introduction to the usage of new operator in js (code)

不言
不言Original
2018-08-23 14:20:591819browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn