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

vue-admin-template dynamic routing implementation example

Provide login and obtain user information data in...

10 tips for designing useful, easy-to-use web applications

Here are 10 tips on how to design better-usable w...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Achieve 3D flip effect with pure CSS3 in a few simple steps

As a required course for front-end developers, CS...

How to use mysqldump for full and point-in-time backups

Mysqldump is used for logical backup in MySQL. Al...

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

Introduction to JavaScript Number and Math Objects

Table of contents 1. Number in JavaScript 2. Math...

Mac+IDEA+Tomcat configuration steps

Table of contents 1. Download 2. Installation and...

Implementation of breakpoint resume in vue-video-player

In a recent project, I needed to implement the fu...

Design Theory: Textual Expression and Usability

<br />In text design, we usually focus on th...

Practice of Vue global custom instruction Modal drag

Table of contents background Implementation ideas...

Complete steps for uninstalling MySQL database

The process of completely uninstalling the MySQL ...