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

Problems and solutions of using jsx syntax in React-vscode

Problem Description After installing the plugin E...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

js version to realize calculator function

This article example shares the specific code of ...

How to create LVM for XFS file system in Ubuntu

Preface lvm (Logical Volume Manager) logical volu...

Vue components dynamic components detailed explanation

Table of contents Summarize Summarize When the ar...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

CSS realizes the realization of background image screen adaptation

When making a homepage such as a login page, you ...

Implementation of mysql decimal data type conversion

Recently, I encountered a database with the follo...

Example of Vue transition to achieve like animation effect

Table of contents Results at a Glance Heart Effec...

Detailed explanation of how to pass password to ssh/scp command in bash script

Install SSHPASS For most recent operating systems...

Vue parent-child component mutual value transfer and call

Table of contents 1. Parent passes value to child...