In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces what are the function parameters and side effects of JavaScript code, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Functions are an important part of JavaScript programs, which are used to divide code into reusable blocks. Therefore, in order to have clean JavaScript code, we need functions that are easy to understand.
This article will introduce more attributes of high-quality functions, including flag parameters, binary and ternary functions, and side effects.
Mark parameter
Boolean parameters should be used with caution. It makes the function signature more complex and tells us that the function does more than one thing (with multiple paths).
Binary function
Binary functions are more difficult to understand than functions that take fewer arguments. But sometimes it makes sense to use binary functions. For example, if you have an object that holds Cartesian coordinates, it should have two parameters.
For example, you can create a class with a constructor that takes two parameters, as follows:
Class Point {constructor (x, y) {this.x = x; this.y = y;}} const point = new Point (1,2)
It is almost impossible to define it in other ways.
However, we must realize that binary functions require more time and effort than functions that use fewer arguments.
Ternary function
A function with three parameters takes a lot of time and effort to understand a function with two parameters.
If there are 2 or fewer parameters, there are more combinations of parameters to consider.
Merge parameters into objects
If a function contains multiple parameters, you should consider merging them into objects.
If there is a correlation between parameters, it is more appropriate to do so. For example, the following function contains many parameters:
Const describeFruit = (color,name, size, price, numSeeds, type) = > {return `${fruitName} is ${fruitColor}. It's ${fruitSize}. It costs ${price}. It has ${numSeeds}. The type if ${type} `;}
Six parameters may be too many, and you can clean it up by passing in an object:
Const describeFruit = (fruit) = > {return `${fruit.name} is$ {fruit.color}. It's ${fruit.size}. It costs ${fruit.price}. It has$ {fruit.numSeeds}. The type if ${fruit.type} `;}
As we can see, it is cleaner and does not have to worry about the passing of a large number of parameters.
Because the function is relatively short, it is also more suitable for screen display.
Five parameters may be the maximum value that a function should contain.
Verbs and keywords
It's a good idea to include verbs and keywords in the function name because they do something, which means that the verbs in the name are reasonable.
In addition, we need to know who is performing the operation. This means that some keywords must be added to do this.
For example, a high-quality function definition that meets this point is similar to:
Const copyArray = (array) = > [... array]
The copyArray name lets us know that the function copied an array.
It also lets us know what to pass to the function, which is obviously an array.
No side effects
The side effect is that the code in the function changes what is outside the function.
This is bad because it makes hidden changes to things outside the function.
We should avoid this as much as possible, because it can cause something unexpected and take more time to test, because in addition to accepting parameters, performing actions, and returning results, it also makes changes to functions that must be considered.
This means that we have to test something other than the result returned by the function.
For example, if there are:
Let numFruits = 1; const addFruit = () = > {numFruits++;} const removeFruit = () = > {numFruits--;}
So we have two functions with side effects because they both change the numFruits variables outside their respective functions.
A better way to write these functions is to write them as pure functions. A pure function is a function that returns the same content with the same parameters passed in. And it has no side effects.
As a result, pure functions are easier to test, and their behavior is predictable.
Rewrite the above code as follows:
Let numFruits = 1; const addFruit = (numberOfFruits) = > numberOfFruits + 1; const removeFruit = (numberOfFruits) = > numberOfFruits-1 num fruits = addFruit (numFruits); numFruits = removeFruit (numFruits)
Now, there are two functions that take the numFruits parameter and return a larger or smaller number, respectively.
You can then use them to change the numFruits variable outside the function.
As we can see, they do nothing with numFruits, but instead return the numberOfFruits parameter plus or minus 1, respectively.
If we write tests for them, we can easily test them by passing in the input and checking that the output is what we want. This is much better than assigning side effects to variables that may be applicable to the test code.
Flag parameters should be minimized. They tell us that this function not only accomplishes one thing, but also is another parameter in the function signature.
Functions that use fewer arguments are better than functions that use more arguments. If you need many parameters, consider merging them into a single object.
Finally, if conditions permit, side effects should be avoided as far as possible. Functions with side effects perform hidden operations and are difficult to test. Pure functions have no side effects, so they are more testable and predictable.
About the function parameters and side effects of JavaScript code what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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: 209
*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.