In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what shorthand skills does JavaScript have". In daily operation, I believe many people have doubts about what shorthand skills JavaScript has. 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 doubts about "what shorthand skills JavaScript has". Next, please follow the editor to study!
1. Variable assignment
When assigning the value of one variable to another, you first need to make sure that the original value is not null, undefined, or null.
This can be achieved by writing a judgment statement that contains multiple conditions:
Or abbreviated to the following form:
You can paste the following code into es6console and test it yourself:
two。 Default value assignment
If the expected parameter is null or undefined, you do not need to write six lines of code to assign the default value. We can do the same thing with a single line of code using only a short logical operator.
Abbreviated as:
3. Object attribute
ES6 provides a very simple way to assign attributes to objects. If the property name is the same as the key name, you can use an abbreviation.
Abbreviated as:
4. Arrowhead function
Classic functions are easy to read and write, but if you nest them in other functions and call them, the whole function becomes a bit verbose and confusing. At this point, you can use the arrow function to abbreviate:
Abbreviated as:
5. Implicit return value
The return value is the keyword that we usually use to return the final result of the function. The arrow function with only one statement can implicitly return the result (the function must omit parentheses ({}) in order to omit the return keyword).
To return a multiline statement, such as object text, you need to wrap the function body with () instead of {}. This ensures that the code evaluates as a single statement.
Abbreviated as:
6. Default parameter valu
You can use the if statement to define the default values for function parameters. ES6 specifies that default values can be defined in function declarations.
Abbreviated as:
7. Template string
In the past, we are used to using "+" to convert multiple variables into strings, but is there an easier way?
ES6 provides the appropriate methods, and we can use backquotes and ${} to combine variables into a string.
Abbreviated as:
8. Deconstruction assignment
Deconstructing an assignment is an expression that is used to quickly extract attribute values from an array or object and assign them to defined variables.
In the aspect of code shorthand, deconstructing assignment can achieve good results.
Abbreviated as:
You can even specify your own variable name:
9. Unfold operator
The unfold operator was introduced in ES6, and using the unfold operator can make JavaScript code more efficient and interesting.
Use the expansion operator to replace some array functions.
Abbreviated as:
Unlike the concat () function, users can use the extension operator to insert another array into any array.
You can also use the unfold operator with ES6 deconstruction symbols:
10. Forced parameter
By default, if you do not pass a value to a function parameter, JavaScript sets the function parameter to undefined. Some other languages issue warnings or errors. To perform parameter assignment, you can use if statements to throw undefined errors, or you can use "force parameters".
Abbreviated as:
11. Array.find
If you have ever written a find function in a normal JavaScript, you may have used a for loop. In ES6, a new array function called find () is introduced, which can implement the abbreviation of for loop.
Abbreviated as:
12. Object [key]
Although it is a common practice to write foo.bar as foo ['bar'], it forms the basis for writing reusable code.
Consider the following simplified example of a validation function:
The above function * * completes the verification work. But when there are many forms, you need to apply validation, and there are different fields and rules. It would be a good choice if you could build a generic validation function that is configured at run time.
Universal validation function
Now that we have this validation function, we can reuse it in all forms without having to write custom validation functions for each form.
13. Two-bit operator
Bit operators are the basic knowledge points of the JavaScript introductory tutorial, but we don't often use bit operators. Because no one wants to use 1s and zeros without dealing with binaries.
But the two-bit operator has a very practical case. You can use the two-bit operator instead of Math.floor (). The advantage of the double no positioning operator is that it performs the same operation faster.
Math.floor (4.9) = 4 / / true
Abbreviated as:
~ 4.9 = = 4 / / true
At this point, the study of "what are the shorthand skills of JavaScript" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.