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

Four tips to improve development efficiency in JS

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

Share

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

This article will explain in detail the four tips for improving development efficiency in JS. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Short circuit judgment

You can use this method when only simple if conditions are needed

Let x = 0 foo = () = > console.log ('executed'); if (x = 0) {foo ()}

To achieve the same if function by using the & & operator, if the condition before & & is false, the code after & & will not be executed.

Let x = 0 foo = () = > console.log ('executed'); x = 0 & & foo ()

You can also add more if conditions, but this also increases the complexity of the statement, and more than 2 conditions are not recommended.

Let x = 0 foo = () = > console.log ('executed'), x = 0 & & y = 0 & & foo () 2, optional chain operator (?)

We often judge whether a key exists in the JS object, because sometimes we are not sure whether the data returned by the background API is correct.

The user object contains an object with the attribute name, and the name object has an attribute firstName. If you use user.name.firstName to judge directly, if the name attribute does not exist, it will report an error. Therefore, before judging, you still need to judge whether the user.name exists or not. You need to nest two layers of if judgment.

Let user = {name: {firstName: 'Avedos'} if (user.name) {if (user.name.firstName) {console.log ('user object contains firstName fields')}}

Can we use it at this time? Operator to simplify the operation, and if user.name does not exist, it will also return false, so use a layer of judgment directly

Let user = {name: {firstName: 'Avantis'} if (user.name?.firstName) {console.log ('user object contains firstName fields')} 3, null merge operator (?)

The ternary operator is shorter than if/else. If the logic is simple, it is easy to use.

For example:

Let user = {name: {firstName: 'Aphrodite'} let foo = () = > {return user.name?.firstName? User.name.firstName: 'firstName does not exist'} console.log (foo ())

Use it first? Operator to determine whether it exists. If it exists, it returns. If it does not exist, it returns false. Enter the following logic.

Use?? The algorithm makes the code more concise

Let user = {name: {firstName: 'Avoreans'} let foo = () = > {return user.name?.firstName? 'firstName does not exist'} console.log (foo ()) 4, return termination function

The following function determines the value of x, using a large number of if else nesting

Let x = 1 foo = () = > {if (x)

< 1){ return 'x 小于 1' } else { if(x >

1) {return'x greater than 1'} else {return'x equals 1'}} console.log (foo ())

This if else nesting simplifies the code by removing the else condition because the return statement terminates code execution and returns the function.

Let x = 1 foo = () = > {if (x)

< 1){ return 'x 小于 1' } if(x >

1) {return'x greater than'} return'x equals 1'} console.log (foo ()) this is the end of this article on "four Tips to improve Development efficiency in JS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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