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 analyze the principle of new operator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What this article shares to you is about how to analyze the principle of the new operator. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Front-end interview-the principle of new operator interprets new examples

The new operator in JavaScript is used to create an object instance of a given constructor.

Function Person (name, age) {this.name = name; this.age = age;} const res = new Person ('Tom', 20) console.log (res) / / Person {name: "Tom", age: 20} prototype, constructor, instance relationship

Prototype: a simple object that implements property inheritance of an object. It can be simply understood as the father of the object. In Firefox and Google, each js object contains a * * proto** (non-standard) attribute pointing to its father (the object prototype) that can be accessed by obj.__proto__.

Constructor: you can create a new function for an object through new.

Example: an object created by constructor and new is an instance. The instance points to the prototype through * * proto and to the constructor through constructor**.

Therefore, we can see an example of the relationship between the three. _ _ proto__=== prototype. Constructor = constructor. Prototype = prototype instance. Constructor = constructor new operator implementation principle

Generate a new object

Link to prototype

Bind this

Returns a new object (if the constructor has its own retrun, this value)

Function myNew (constrc,... args) {const obj = {}; / / 1. Create an empty object obj.__proto__ = constrc.prototype; / / 2. Point the [[prototype]] attribute of obj to the prototype object const result = constrc.apply (obj, args) of the constructor; / / 3. Bind the context this executed by constrc to obj and execute return result instanceof Object? Result: obj; / / 4. If the constructor returns an object, the result executed by the constructor is used. Otherwise, return the newly created object} / / example: function Person (name, age) {this.name = name; this.age = age;} const res = myNew (Person, 'Tom', 20) console.log (res) / / Person {name: "Tom", age: 20} is how to analyze the principle of the new operator. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report