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 increase the readability of code in JavaScript

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

Share

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

This article introduces the relevant knowledge of "what are the ways to increase the readability of code in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Strongly typed check

Replace = with =

two。 Variable

Name the variable in a well-known way, so that when you see the variable name again, you can roughly understand its meaning

Do not add extra unwanted words to the variable name

Do not abbreviate variable context

Do not add unnecessary context.

3. Function

Use a long and descriptive name, considering that the function represents a behavior, the function name should be a verb or phrase to describe the intention behind it and the intention of the parameter. The name of the function should indicate what they did.

Avoid using a large number of arguments, and ideally, the function should specify two or fewer arguments. The fewer parameters, the easier it is to test the function, and objects can be used in the case of more parameters.

Use default parameter instead of | | Action

A function should only do one thing, do not perform multiple operations in a function

Use Object.assign to set object defaults

Do not use flags as arguments, because they tell the function to do more than it should

Do not pollute global variables. If you need to extend existing objects, use ES6 classes and inheritance instead of creating functions on the prototype chain of native objects

4. Conditions

Avoid using negative conditions

Use conditional abbreviation, use this method only for Boolean values, and use this method if you are sure that the value will not be undefined or null

Avoid conditionals as much as possible, but instead use polymorphism and inheritance

5. Class

Class is the new syntax candy in JS. It works like the previous prototype, but it is easier to understand than the prototype.

With links, many libraries, such as jQuery and Lodash, use this mode. In a class, you can link more methods of this class to it simply by returning this at the end of each function.

This is the end of the content of "what are the ways to increase the readability of code in JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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