Detailed explanation of the principle and example of the new operator in JavaScript

Detailed explanation of the principle and example of the new operator in JavaScript

Uses of new

The function of new is to create an instance object through a constructor. The relationship between the instance, the prototype and the constructor is shown in the following figure:

Let’s summarize first

  1. Create an empty object
  2. The internal property __proto__ of the empty object is assigned to the prototype property of the constructor function.
  3. Set the constructor's this to point to an empty object
  4. Execute the code inside the constructor
  5. Return the new object

Detailed description

When executing the new operation, the following steps are performed in sequence:

1. Create an empty object

  • The empty object is an instance of Object, that is, {}.
let obj = {}

2. The internal property __proto__ of the empty object is assigned to the prototype property of the constructor

  • This operation is to link the empty object to the correct prototype
function Foo(num) {
  this.number = num
}

obj.__proto__ = Foo.prototype

3. Point the constructor's this to an empty object

  • That is, this inside the constructor is assigned to an empty object so that the constructor can be executed correctly later.
Foo.call(obj, 1)

4. Execute the code inside the constructor

  • That is, add properties and methods to the empty object.

5. Return the new object

  • If a reference type value is returned through the return statement inside the constructor, the new operation will eventually return this reference type value; otherwise, it will return the newly created object.
  • Reference type values: all values ​​except primitive type values ​​(numbers, strings, Boolean values, null, undefined, and Symbol values).

Emulating the new operator

The following myNew function simulates the behavior of the new operator:

function myNew(func, ...args) {
  let obj = {}
  obj.__proto__ = func.prototype
  let res = func.apply(obj, args)
  return res instanceof Object ? res : obj
}

function Foo(num) {
  this.number = num
}

let foo1 = myNew(Foo, 1)
console.log(foo1 instanceof Foo) // true
console.log(foo1.number) // 1

let foo2 = new Foo(2)
console.log(foo2 instanceof Foo) // true
console.log(foo2.number) // 2

The instanceof operator is used above to determine whether the return value of the constructor is an instance of Object, that is, whether it is a reference type value; this is because all reference type values ​​are instances of Object, and Object is the base type of all reference type values.

Well, this is the end of this article about the principle of the new operator in JavaScript. For more relevant content about the principle of JS new operator, 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
  • Research on two ways to implement new in JavaScript
  • How to implement JavaScript's new operator yourself
  • Detailed explanation of the role of the new operator in Js
  • 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

<<:  Create a virtual environment using venv in python3 in Ubuntu

>>:  Steps to install superset under win10 system

Recommend

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

Use pictures to realize personalized underline of hyperlinks

Don't be surprised if you see some kind of und...

React-native sample code to implement the shopping cart sliding deletion effect

Basically all e-commerce projects have the functi...

Linux uses if to determine whether a directory exists.

How to use if in Linux to determine whether a dir...

Mini Program to Implement Calculator Function

This article example shares the specific code of ...

How to handle forgotten passwords in Windows Server 2008 R2

What to do if you forget Windows Server 2008R2 So...

How to quickly clean up billions of data in MySQL database

Today I received a disk alarm exception. The 50G ...

A brief analysis of the responsiveness principle and differences of Vue2.0/3.0

Preface Since vue3.0 was officially launched, man...

Detailed discussion of memory and variable storage in JS

Table of contents Preface JS Magic Number Storing...

Detailed graphic explanation of mysql query control statements

mysql query control statements Field deduplicatio...

Summary of Creating and Using Array Methods in Bash Scripts

Defining an array in Bash There are two ways to c...