最好的现代javascript默认参数

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函数采用具有属性abc

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
Photo by Matt Hardy on Unsplash
马特·哈迪( Matt Hardy)Unsplash

结论(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频道解码,以获得更多类似的内容

翻译自: https://medium.com/javascript-in-plain-english/best-of-modern-javascript-default-parameters-a40d7d2691c1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值