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 tips for ES6?

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what ES6 tips are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Mandatory parameter

ES6 provides a default parameter value mechanism that allows you to set default values for parameters to prevent them from being passed in when the function is called.

In the following example, we write a required () function as the default value for the parameters an and b. This means that if one of the arguments an or b does not pass a value on the call, the required () function is defaulted and an error is thrown.

Const required = () = > {throw new Error ('Missing parameter')}; const add = (a = required (), b = required ()) = > a + 3add (1) / / Error: Missing parameter.2. Powerful reduce

The reduce method of an array has a wide range of uses. It is generally used to reduce each item in an array to a single value. But you can do more with it.

2.1Using reduce to implement both map and filter

Suppose there is a sequence now, and you want to update each item (the function of map) and filter out a portion (the function of filter). If you use map first and then filter, you need to iterate through the array twice.

In the following code, we double the values in the sequence and pick out the numbers that are greater than 50. Notice how we use reduce very efficiently to accomplish both the map and filter methods?

Const numbers = [10,20,30,40]; const doubledOver50 = numbers.reduce ((finalList, num) = > {num = num * 2; if (num > 50) {finalList.push (num);} return finalList;}, []); doubledOver50; / / [60,80] 2.2 replace map and filter with reduce

If you read the above code carefully, you should be able to understand that reduce can replace map and filter.

2.3 use reduce to match parentheses

Another use of reduce is to be able to match parentheses in a given string. For a string with parentheses, we need to know whether the number of parentheses is the same and whether it appears before.

We can easily solve this problem with reduce in the following code. We just need to declare a counter variable with an initial value of 0. When you encounter (counter plus one, encounter), counter minus one. If the number of left and right parentheses matches, the final result is 0.

/ / Returns 0 if balanced.const isParensBalanced = (str) = > {return str.split (''). Reduce ((counter, char) = > {if (counter < 0) {/ / matched ")" before "(" return counter;} else if (char ='(') {return + + counter;} else if (char =')) {return-- counter;} else {/ / matched some other char return counter;}, 0) / / 3) / / [4,5] 5. Array deconstruction

Sometimes you put multiple values returned by a function in an array. We can use array deconstruction to get each of these values.

5.1Numeric exchange let param1 = 1 param1 param2 = 2 param1 swap and assign param1 & param2 each others values [param1, param2] = [param2, param1]; console.log (param1) / / 2console.log (param2) / / 15.2 receive multiple results returned by the function

In the following code, we get a post from / post and then get the relevant comments in / comments. Since we are using async/await, the function puts the return value in an array. After we deconstruct the array, we can assign the return value directly to the corresponding variable.

Async function getFullPost () {return await Promise.all ([fetch ('/ post'), fetch ('/ comments')]);} const [post, comments] = getFullPost (); these are all the contents of the article "what are the ES6 tips?" 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