Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the new operator in JS

Shulou Source: shulou.com Published: 2022-06-02 17:20:49 11月18日 Update

This article mainly explains "how to use the new operator in JS". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the new operator in JS".

First, let's take a look at what the browser does by instantiating an object.

Four steps of new:

1. Create an empty object

two。 Set the proto property of the empty object to inherit the prototype property of the constructor, that is, the public properties and methods on the prototype object that inherits the constructor

3. Call the constructor, replace the this in the constructor with the this of the empty object, and inherit the properties in the constructor

4. Returns a new object inside the function

Web front-end development learning Q-q-u-n: 784783012, sharing development tools, zero basics, advanced video tutorials, hope beginners take fewer detours

Source code implementation

Function myNew (fun) {return function () {/ / create a new object and point its implicit prototype to the constructor prototype let obj = {_ _ proto__: fun.prototype} / / execute the constructor fun.call (obj,... arguments) / / return the object return obj}} function person (name) Age) {this.name = name this.age = age} let obj = myNew (person) ('chen', 18) / / {name: "chen", age: 18} Thank you for reading The above is the content of "how to use the new operator in JS". After the study of this article, I believe you have a deeper understanding of how to use the new operator in JS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Functions objects operators properties learning prototypes content development that is things front ends foundations instances that is tools development tools ideas situations directions tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information OPPO Reno Xiaomi Microsoft