Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the new operator in JS

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report