2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b8e367417b2b2512b5f
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 传递参数以避免函数中的外部依赖
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
forumTopicId: 301234
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: pass-arguments-to-avoid-external-dependence-in-a-function
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
上一个挑战是更接近函数式编程原则的挑战,但是仍然缺少一些东西。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
虽然我们没有改变全局变量值,但在没有全局变量`fixedValue`情况下,`incrementer`函数将不起作用。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
函数式编程的另一个原则是:总是显式声明依赖关系。如果函数依赖于一个变量或对象,那么将该变量或对象作为参数直接传递到函数中。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
这样做会有很多好处,其中一点是让函数更容易测试,因为你确切地知道参数是什么,并且这个参数也不依赖于程序中的任何其他内容。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
其次,这样做可以让你更加自信地更改,删除或添加新代码。因为你很清楚哪些是可以改的,哪些是不可以改的,这样你就知道哪里可能会有潜在的陷阱。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
最后,无论代码的哪一部分执行它,函数总是会为同一组输入生成相同的输出。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
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
|
|
|
更新`incrementer`函数,明确声明其依赖项。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
编写`incrementer`函数,获取它的参数,然后将值增加 1。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`incrementer`函数不能修改`fixedValue`的值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(fixedValue === 4);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`incrementer`函数应该接收一个参数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(incrementer.length === 1);
|
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`incrementer`函数应返回比`fixedValue`更大的值。
|
2020-08-05 16:38:04 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(newValue === 5);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// The global variable
|
|
|
|
var fixedValue = 4;
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
function incrementer () {
|
|
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
// The global variable
|
|
|
|
var fixedValue = 4;
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
function incrementer (fixedValue) {
|
|
|
|
return fixedValue + 1;
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|