2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b88367417b2b2512b44
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 编写带参数的箭头函数
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-08-04 15:13:35 +08:00
|
|
|
forumTopicId: 301223
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: write-arrow-functions-with-parameters
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2020-08-04 15:13:35 +08:00
|
|
|
和一般的函数一样,你也可以给箭头函数传递参数。
|
|
|
|
|
|
|
|
```js
|
|
|
|
// 给传入的数值乘以 2 并返回结果
|
|
|
|
const doubler = (item) => item * 2;
|
|
|
|
```
|
|
|
|
|
|
|
|
如果箭头函数只有一个参数,则可以省略包含该参数的括号。
|
|
|
|
|
|
|
|
```js
|
|
|
|
// the same function, without the argument parentheses
|
|
|
|
const doubler = item => item * 2;
|
|
|
|
```
|
|
|
|
|
|
|
|
可以将多个参数传递到箭头函数中。
|
|
|
|
|
|
|
|
```js
|
|
|
|
// multiplies the first input value by the second and returns it
|
|
|
|
const multiplier = (item, multi) => item * multi;
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
使用箭头函数的语法重写`myConcat`函数,使其可以将`arr2`的内容填充在`arr1`里。
|
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
替换掉所有的`var`关键字。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
(getUserInput) => assert(!getUserInput('index').match(/var/g));
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`myConcat`应该是一个常量 (使用`const`)。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
(getUserInput) => assert(getUserInput('index').match(/const\s+myConcat/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`myConcat`应该是一个函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(typeof myConcat === 'function');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`myConcat()` 应该返回 `[1, 2, 3, 4, 5]`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(() => {
|
|
|
|
const a = myConcat([1], [2]);
|
|
|
|
return a[0] == 1 && a[1] == 2;
|
|
|
|
});
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
不要使用`function`关键字。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
(getUserInput) => assert(!getUserInput('index').match(/function/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:13:35 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
var myConcat = function(arr1, arr2) {
|
|
|
|
return arr1.concat(arr2);
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(myConcat([1, 2], [3, 4, 5]));
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
const myConcat = (arr1, arr2) => {
|
|
|
|
return arr1.concat(arr2);
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(myConcat([1, 2], [3, 4, 5]));
|
|
|
|
```
|