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 new () in Javascript

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use new () in Javascript". In daily operation, I believe many people have doubts about how to use new () in Javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use new () in Javascript". Next, please follow the editor to study!

To create a new instance of Person, you must use the new operator.

Calling the constructor in this way actually goes through the following four steps:

(1) create a new object

(2) assign the scope of the constructor to the new object (so this points to the new object)

(3) execute the code in the constructor (add properties to this new object)

(4) return the new object.

New operator

With the introduction of the above basic concepts and the addition of the new operator, we can complete the traditional object-oriented class + new way of creating objects, which we call Pseudoclassical in JavaScript.

Based on the above example, we execute the following code

Var obj = new Base ()

What is the result of this code? the object model we see in the Javascript engine is:

What exactly does the new operator do? In fact, it's very simple. I just did three things.

Var obj = {}; obj.__proto__ = Base.prototype;Base.call (obj)

In the first line, we create an empty object, obj

In the second line, we point the _ _ proto__ member of the empty object to the Base function object prototype member object

In the third line, we replace the this pointer of the Base function object with obj, and then call the Base function, so we assign an id member variable to the obj object, whose value is "base", about the use of the call function.

What if we add some functions to the Base.prototype object?

For example, the code is as follows:

Base.prototype.toString = function () {return this.id;}

So when we use new to create a new object, according to the nature of _ _ proto__, the toString method can also be accessed as a new object method. So we saw:

In the constructor, we set the member variables of the class (for example, id in the example), and in the construction child object prototype, we set the public methods of the class. So the effect of class and class instantiation is simulated through function objects and Javascript-specific _ proto__ and prototype members and new operators.

At this point, the study on "how to use new () in Javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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