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)

不言
Release: 2018-08-23 14:20:59
Original
1825 people have browsed it

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 ();
Copy after login

new has gone through four stages:
1. Create an empty object

varobj=new Object();
Copy after login

2. Set up the prototype chain

obj.__proto__= Func.prototype;
Copy after login

3. Let this in Func point to obj and execute the function body of Func.

var result =Func.call(obj);
Copy after login

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;;
}
Copy after login

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!

Related labels:
new
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template