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

What are the ways to create objects in javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what are the ways to create objects in javascript", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the ways to create objects in javascript" this article.

How many ways does javascript create objects?

We usually create objects directly in the form of literals, but this way of creation produces a lot of repetitive code when creating a large number of similar objects. But js is different from a general object-oriented language in that it didn't have the concept of classes before ES6. But we can use functions to simulate, resulting in reusable ways to create objects, and there are several ways I've learned:

The first is the factory pattern, the main working principle of the factory pattern is to use functions to encapsulate the details of creating objects, so as to achieve the purpose of reuse by calling functions. But it has a big problem is that the created object can not be associated with a type, it simply encapsulates the reuse code, but does not establish the relationship between the object and the type.

The second is the constructor mode. Every function in js can be used as a constructor, as long as a function is called through new, then we can call it a constructor. Executing the constructor first creates an object, then points the prototype of the object to the constructor's prototype property, then points the this in the execution context to the object, and finally executes the entire function, and returns the newly created object if the return value is not the object. Because the value of this points to the newly created object, we can use this to assign values to the object. The advantage of the constructor pattern over the factory pattern is that the object created is related to the constructor, so we can identify the type of the object through the prototype. But a disadvantage of the constructor is that it results in unnecessary creation of the function object, because the function is also an object in js, so if the object property contains a function, then every time we will create a new function object, wasting unnecessary memory space, because the function is universal for all instances.

The third pattern is the prototype pattern, because each function has a prototype property, which is an object that contains properties and methods that can be shared by all instances created through the constructor. So we can use prototype objects to add common properties and methods to achieve code reuse. Compared with the constructor mode, this method solves the problem of reuse of function objects. But there are some problems with this mode, one is that there is no way to initialize the value by passing in parameters, and the other is that if there is a value of a reference type such as Array, then all instances will share an object, and the change of one instance to the value of the reference type will affect all instances.

The fourth pattern is a combination of constructor pattern and prototype pattern, which is the most common way to create custom types. Because there are some problems in the separate use of constructor pattern and prototype pattern, we can combine these two patterns to initialize the properties of objects through constructors and reuse function methods through prototype objects. This method solves the disadvantage of using the two modes alone, but one deficiency is that the encapsulation of the code is not good enough because of the use of two different modes.

The fifth mode is the dynamic prototype pattern, which moves the creation process of the prototype method assignment to the interior of the constructor by judging whether the attribute exists or not. it can achieve the effect of assigning values to the prototype object only once when the function is called for the first time. This approach encapsulates the above hybrid mode very well.

The sixth pattern is the parasitic constructor pattern, which is basically the same as the factory pattern. My understanding of this pattern is that it is mainly based on an existing type and extends the instantiated object when instantiated. In this way, there is no need to modify the original constructor, but also to achieve the purpose of extending the object. One of its disadvantages, like the factory pattern, is that it cannot identify objects.

The above is all the content of the article "what are the ways to create objects in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Development

Wechat

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

12
Report