A function can accept one or more parameters.
一个函数可以接受一个或多个参数。
const dosomething = () => {
//do something
}
const dosomethingElse = foo => {
//do something
}
const dosomethingElseAgain = (foo, bar) => {
//do something
}
Starting with ES6/ES2015, functions can have default values for the parameters:
从ES6 / ES2015开始 ,函数可以具有参数的默认值:
const dosomething = (foo = 1, bar = 'hey') => {
//do something
}
This allows you to call a function without filling all the parameters:
这使您可以在不填充所有参数的情况下调用函数:
dosomething(3)
dosomething()
ES2018 introduced trailing commas for parameters, a feature that helps reducing bugs due to missing commas when moving around parameters (e.g. moving the last in the middle):
ES2018引入了参数的尾部逗号,该功能有助于减少在参数周围移动(例如,在中间移动最后一个)时缺少逗号引起的错误:
const dosomething = (foo = 1, bar = 'hey',) => {
//do something
}
dosomething(2, 'ho!')
It is also okay to call your functions with a trailing comma after the last parameter:
它也还可以与最后一个参数后尾随逗号打电话给你的功能:
dosomething(2, 'ho!',)
You can wrap all your arguments in an array, and use the spread operator when calling the function:
您可以将所有参数包装在数组中,并在调用函数时使用spread运算符 :
const dosomething = (foo = 1, bar = 'hey') => {
//do something
}
const args = [2, 'ho!']
dosomething(...args)
With many parameters, remembering the order can be difficult. Using objects, destructuring allows to keep the parameter names:
使用许多参数,记住顺序可能很困难。 使用对象,解构可以保留参数名称:
const dosomething = ({ foo = 1, bar = 'hey' }) => {
//do something
console.log(foo) // 2
console.log(bar) // 'ho!'
}
const args = { foo: 2, bar: 'ho!' }
dosomething(args)
Functions now support default parameters:
函数现在支持默认参数:
const foo = function(index = 0, testing = true) { /* ... */ }
foo()
Default parameter values have been introduced in ES2015, and are widely implemented in modern browsers.
默认参数值已在ES2015中引入,并已在现代浏览器中广泛实现。
This is a doSomething
function which accepts param1
.
这是一个接受参数param1
的doSomething
函数。
const doSomething = (param1) => {
}
We can add a default value for param1
if the function is invoked without specifying a parameter:
如果在不指定参数的情况下调用函数,我们可以为param1
添加默认值:
const doSomething = (param1 = 'test') => {
}
This works for more parameters as well, of course:
当然,这也适用于更多参数:
const doSomething = (param1 = 'test', param2 = 'test2') => {
}
What if you have an unique object with parameters values in it?
如果您有一个带有参数值的唯一对象怎么办?
Once upon a time, if we had to pass an object of options to a function, in order to have default values of those options if one of them was not defined, you had to add a little bit of code inside the function:
曾几何时,如果必须将选项对象传递给函数,如果未定义其中一个选项,则要具有这些选项的默认值,则必须在函数内部添加一些代码:
const colorize = (options) => {
if (!options) {
options = {}
}
const color = ('color' in options) ? options.color : 'yellow'
...
}
With object destructuring you can provide default values, which simplifies the code a lot:
使用对象分解,您可以提供默认值,从而大大简化了代码:
const colorize = ({ color = 'yellow' }) => {
...
}
If no object is passed when calling our colorize
function, similarly we can assign an empty object by default:
如果在调用我们的colorize
函数时没有传递任何对象,则类似地,我们可以默认分配一个空对象:
const spin = ({ color = 'yellow' } = {}) => {
...
}
翻译自: https://flaviocopes.com/javascript-function-parameters/