Detailed explanation of the role of the new operator in Js

Detailed explanation of the role of the new operator in Js

Preface

Js is the most commonly used code manipulation language today, among which the new operator is particularly common. For many code novices, it is not clear what role new plays in Js, what it is used for, and what it does. This article starts with the role of the new operator and briefly introduces the relevant knowledge of the new operator.

What is new?

As we all know, in JS, the role of new is to create an instance object through a constructor.

Like this: (Unlike ordinary functions, when a function is used as a constructor, the first letter is usually capitalized to distinguish it.)

function Foo(name) {
  this.name = name;
}
console.log("new Foo('mm')'s type:",typeof new Foo('mm')); // object
console.log("Foo's type:",typeof Foo); // function


Creates an empty object

var obj = new Object();

In Js code, the main function of the new operator is to generate objects. Create an empty object through new to lay the foundation for creating objects.

Setting up the prototype chain

obj.__proto__ = Func.prototype;

After using the new operator to build the base in JS, the next step of JS code operation begins, setting up the prototype chain. The instance created by the new constructor can access the properties in the constructor prototype chain. In other words, through the new operator, the prototype chain links the instance and the construction function.

(Change this pointer) Let this in Func point to obj and execute the function body of Func.

var result =Func.call(obj);

Generally speaking, in a Js code group, when this appears, the constructor works normally, but when the this pointer is changed through the new operator, the return value will be returned normally.

Determine the return value type of Func: If it is a value type, return obj. If it is a reference type, return an object of this reference type.

 if (typeof(result) == "object"){
  func=result;
}
else{
  func=obj;
}

As can be seen from the above set of new operator codes, new can also be used to determine the return value type of Func. If the return value is a value type, it is returned normally. If it is a reference type, it returns an object of the reference type.

The above four points are the main functions of the new operator in Js code. I hope it can be helpful for Js code novices.

Summarize

This is the end of this article about the role of the new operator in Js. For more information about the role of the new operator in Js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Let's talk in depth about the principle and implementation of new in JS
  • Detailed description of the function of new in JS
  • Detailed explanation of the principle and example of the new operator in JavaScript
  • Research on two ways to implement new in JavaScript
  • How to implement JavaScript's new operator yourself
  • Summary of common methods of c# Newtonsoft.Json
  • C# Newtonsoft.Json parses multiple nested json for deserialization example
  • c# add Newtonsoft.Json package operation
  • The new command in JavaScript
  • Handwriting implementation of new in JS

<<:  How to access the local machine (host machine) in Docker

>>:  How to mount a data disk on Tencent Cloud Server Centos

Recommend

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

How to use nginx to access local static resources on Linux server

1. Check whether port 80 is occupied. Generally, ...

Problems and solutions for installing Docker on Alibaba Cloud

question When installing Docker using Alibaba Clo...

How to use Spark and Scala to analyze Apache access logs

Install First you need to install Java and Scala,...

Independent implementation of nginx container configuration file

Create a container [root@server1 ~]# docker run -...

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

How to solve the 10060 unknow error when Navicat remotely connects to MySQL

Preface: Today I want to remotely connect to MySQ...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

MySQL online deadlock analysis practice

Preface I believe that everyone has had a simple ...

How to draw a cool radar chart in CocosCreator

Table of contents Preface Preview text Graphics C...

Simple Mysql backup BAT script sharing under Windows

Preface This article introduces a simple BAT scri...

How to use yum to configure lnmp environment in CentOS7.6 system

1. Installation version details Server: MariaDB S...

Some experience in building the React Native project framework

React Native is a cross-platform mobile applicati...

Let’s talk in detail about how browsers view closures

Table of contents Preface Introduction to Closure...