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:
|
<<: How to access the local machine (host machine) in Docker
>>: How to mount a data disk on Tencent Cloud Server Centos
Problem Description After installing the plugin E...
Table of contents Precautions Necessary condition...
Introduction to Debian Debian in a broad sense re...
Difference between HTML and XHTML 1. XHTML elemen...
Preface Docker has been very popular in the past ...
Uses of new The function of new is to create an i...
This article example shares the specific code of ...
Preface lvm (Logical Volume Manager) logical volu...
Table of contents Summarize Summarize When the ar...
Table of contents Creating OAuth Apps Get the cod...
When making a homepage such as a login page, you ...
Recently, I encountered a database with the follo...
Table of contents Results at a Glance Heart Effec...
Install SSHPASS For most recent operating systems...
Table of contents 1. Parent passes value to child...