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

TortoiseSvn Little Turtle Installation Latest Detailed Graphics Tutorial

There were always problems when installing tortoi...

How to maintain MySQL indexes and data tables

Table of contents Find and fix table conflicts Up...

MySQL database query performance optimization strategy

Optimize queries Use the Explain statement to ana...

Linux ssh server configuration code example

Use the following terminal command to install the...

Detailed tutorial for installing mysql5.7.21 under Windows system

MySQL Installer provides an easy-to-use, wizard-b...

Where is mysql data stored?

MySQL database storage location: 1. If MySQL uses...

Using JS to implement a rotating Christmas tree in HTML

<!DOCTYPE HEML PUBLIC> <html> <hea...

Detailed usage of Linux text search command find

The find command is mainly used to find directori...

Detailed explanation of Javascript event capture and bubbling methods

Table of contents 1. Event Processing Model 1. Ev...

Navicat for MySql Visual Import CSV File

This article shares the specific code of Navicat ...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

Example of adding multi-language function to Vue background management

Table of contents 1. First, configure the main.js...

How to install JDK 13 in Linux environment using compressed package

What is JDK? Well, if you don't know this que...