Since 2015, JavaScript has improved immensely.
自2015年以来,JavaScript取得了巨大的进步。
It’s much more pleasant to use it now than ever.
现在使用它比以往任何时候都更加愉快。
In this article, we’ll look at working with parameters.
在本文中,我们将研究如何使用参数。
默认参数值 (Default Parameter Values)
We can set default parameter values in JavaScript functions since ES6.
自ES6起,我们可以在JavaScript函数中设置默认参数值。
If the value of the parameter is undefined
, then the default value will be set.
如果参数的值是undefined
,那么将设置默认值。
For instance, if we have a function:
例如,如果我们有一个函数:
function foo(x, y = 0) {
return [x, y];
}
Then if we call it by writing foo(1, 3);
, then we get [1, 3]
.
然后,如果我们通过写foo(1, 3);
调用它foo(1, 3);
,则得到[1, 3]
。
If we call it by writing foo(1);
, then we get [1, 0]
.
如果我们通过写foo(1);
调用它foo(1);
,则得到[1, 0]
。
And if we call it by writing foo();
, then we get [undefined, 0]
.
如果我们通过写foo();
调用它foo();
,则得到[undefined, 0]
。
休息参数 (Rest Parameters)
Rest parameters let us get the remaining parameters as an array.
剩余参数让我们以数组的形式获取剩余参数。
For instance,e if we have:
例如,如果我们有:
function bar(pattern, ...args) {
return {
pattern,
args
};
}
Then if we call it by writing format(3, 2, 1);
, we get:
然后,如果我们通过写format(3, 2, 1);
3,2,1)来调用它format(3, 2, 1);
,我们得到:
{ pattern: 3, args: [2, 1] }
解构参数 (Destructuring Parameters)
We can destructure object parameters into variables.
我们可以将对象参数分解为变量。
For instance, we can write:
例如,我们可以这样写:
function baz({
a = 0,
b = -1,
c = 1
} = {}) {
console.log(a, b, c);
//...
}
Our baz
function takes an object with the properties a
, b
, and c
.
我们的baz
函数采用具有属性a
, b
和c
。
We set the default values for them to 0, -1, and 1 respectively.
我们将它们的默认值分别设置为0,-1和1。
If the argument isn’t passed in, then we set it to an object, which will then set those default values.
如果未传入参数,则将其设置为对象,然后将设置这些默认值。
So if we write:
因此,如果我们写:
baz({
a: 10,
b: 30,
c: 2
});
then a
is 10, b
is 30, and c
is 2.
那么a
是10, b
是30, c
是2。
If we write:
如果我们写:
baz({
a: 3
});
then a
is 3, b
is -1 and c
is 1.
那么a
是3, b
是-1, c
是1。
And if we write:
如果我们写:
baz({});
or
要么
baz();
then a
is 0, b
is -1, and c
is 1.
那么a
是0, b
是-1, c
是1。
Whenever any value is absent, it’ll be replaced with the default value.
每当不存在任何值时,它将被默认值替换。
点差算子 (Spread Operator)
The spread operator lets us spread iterable objects into arguments for function calls.
通过散布运算符,我们可以将可迭代对象散布为函数调用的参数。
For instance:
例如:
Math.max(2, 3, 4, 6)
is the same as:
是相同的:
Math.max(...[2, 3, 4, 6])
We can also spread some arguments:
我们还可以散布一些论点:
Math.max(2, ...[3, 4], 6)
In arrays, we can use the spread operator to turn iterable values in array elements.
在数组中,我们可以使用散布运算符在数组元素中转换可迭代的值。
For instance, we can write:
例如,我们可以这样写:
[1,
and get:
并获得:
[1,
参数默认值 (Parameter Default Values)
We can specify default values for parameters.
我们可以指定参数的默认值。
For instance, we can write:
例如,我们可以这样写:
function foo(x, y = 0) {
return [x, y];
}
Then if we call it without 2nd argument, then the default value will be set as the value of y
.
然后,如果我们在没有第二个参数的情况下调用它,则默认值将设置为y
的值。
If we call foo(1)
, then we get [1, 0]
.
如果调用foo(1)
,则得到[1, 0]
。
And if we call foo()
, we get [undefined, 0]
.
如果调用foo()
,则会得到[undefined, 0]
。
The default value is computed only when it’s needed
默认值仅在需要时才计算
We can see this with this example:
我们可以通过以下示例看到这一点:
const log = console.log;function foo(x = log('x'), y = log('y')) {
return [x, y];
}
If we call foo(1)
, then we get 'x'
.
如果我们调用foo(1)
,则得到'x'
。
And if we call foo()
, then we get 'x'
and 'y'
logged.
如果我们调用foo()
,那么我们将记录'x'
和'y'
。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/ec1c709115355e5b3ae3e22aedf89bbe.png)
结论(Conclusion)
We can add default parameters values to functions so that they’re assigned as the value of them when they’re undefined.
我们可以向函数添加默认参数值,以便在未定义它们时将其分配为它们的值。
普通英语JavaScript (JavaScript In Plain English)
Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!
喜欢这篇文章吗? 如果是这样,请订阅我们的YouTube频道解码,以获得更多类似的内容!