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 common js design patterns

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the common js design patterns". In the daily operation, I believe that many people have doubts about the common js design patterns. 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 questions of "what are the common js design patterns?" Next, please follow the editor to study!

What are the common design patterns of js

one。 Singleton mode

Singleton mode is also called singleton mode, and more often called singleton mode. It is a relatively simple but most commonly used design pattern in software design. In JavaScript, there are many ways to implement singletons, and one of the simplest ways is to use the literal quantity of objects, which can contain a large number of properties and methods.

To extend the object, you can add your own private members and methods, and then use closures to encapsulate these variables and function declarations inside. Sample code.

Second, factory model

The factory pattern is a method that determines which class instance is to be created, and these instances often have the same interface. This pattern is mainly used in cases where the instantiated type cannot be determined at compile time, but at execution time.

Example this code comes from the new of es5 and the related instructions of the constructor. New itself is just a process of copying and rewriting an object, and what will be generated is determined by the parameters passed in when calling ObjectFactory.

What else are the common design patterns of js?

Adaptation mode

The main purpose of the adaptation mode is to solve some solutions caused by interface incompatibility. The adapter can provide a unified packaged adapter interface to the user without modifying these incompatible interfaces. On the surface, it feels like the previous facade mode, which wraps and re-presents other objects or interfaces, while the adapter mode tends to solve the compatibility problem, while the facade mode tends to the principle of convenience.

For example, a simple way for students to check their subject scores:

This is a compatible form about adapters to handle parameters. The adapter pattern is simple in the sense of adaptation to solve compatibility problems.

Example 2: the $selector in jquery needs to be changed to $id to match the project, so it's easy to convert $to $id.

Appearance mode

Appearance mode is a relatively simple and ubiquitous mode. The appearance pattern provides a high-level interface that makes it easier for clients or subsystems to invoke. It is represented by a simple piece of code.

At this point, the study of "what are the common js design patterns" 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

Development

Wechat

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

12
Report