2018-10-10 18:03:03 -04:00
---
id: 587d7b8f367417b2b2512b60
2021-03-14 21:20:39 -06:00
title: 在函数中重构全局变量
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-05 16:38:04 +08:00
forumTopicId: 301235
2021-01-13 03:31:00 +01:00
dashedName: refactor-global-variables-out-of-functions
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-03-14 21:20:39 -06:00
目前为止,我们已经看到了函数式编程的两个原则:
2020-12-16 00:37:30 -07:00
2021-11-06 08:56:52 -07:00
1) 不要更改变量或对象 - 创建新变量和对象,并在需要时从函数返回它们。 提示:使用类似 `const newArr = arrVar` 的东西,其中 `arrVar` 是一个数组,只会创建对现有变量的引用,而不是副本。 所以更改 `newArr` 中的值会同时更改 `arrVar` 中的值。
2020-12-16 00:37:30 -07:00
2021-03-14 21:20:39 -06:00
2) 声明函数参数 - 函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。
2020-12-16 00:37:30 -07:00
2021-03-14 21:20:39 -06:00
给数字增加 1 不够刺激,我们可以在处理数组或更复杂的对象时应用这些原则。
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
2021-03-14 21:20:39 -06:00
重构代码,使全局数组 `bookList` 在函数内部不会被改变。 `add` 函数可以将指定的 `bookName` 增加到数组末尾并返回一个新的数组(列表)。 `remove` 函数可以从数组中移除指定 `bookName` 。
2021-02-06 04:42:36 +00:00
2021-03-14 21:20:39 -06:00
**注意:** 两个函数都应该返回一个数组,任何新参数都应该在 `bookName` 参数之前添加。
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
2021-03-14 21:20:39 -06:00
`bookList` 应等于 `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]` .
2018-10-10 18:03:03 -04:00
```js
2022-03-23 19:52:04 +05:30
add(bookList, "Test");
2022-04-07 14:56:53 +05:30
remove(bookList, "The Hound of the Baskervilles");
2020-12-16 00:37:30 -07:00
assert(
JSON.stringify(bookList) ===
JSON.stringify([
'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
2018-10-10 18:03:03 -04:00
```
2022-03-23 19:52:04 +05:30
`add(bookList, "A Brief History of Time")` 应该返回 `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
2022-03-23 19:52:04 +05:30
JSON.stringify(add(bookList, "A Brief History of Time")) ===
2020-12-16 00:37:30 -07:00
JSON.stringify([
'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae',
'A Brief History of Time'
])
);
```
2018-10-10 18:03:03 -04:00
2022-03-23 19:52:04 +05:30
`remove(bookList, "On The Electrodynamics of Moving Bodies")` 应该返回 `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
2022-03-23 19:52:04 +05:30
JSON.stringify(remove(bookList, 'On The Electrodynamics of Moving Bodies')) ===
2020-12-16 00:37:30 -07:00
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
```
2018-10-10 18:03:03 -04:00
2022-03-23 19:52:04 +05:30
`remove(add(bookList, "A Brief History of Time"), "On The Electrodynamics of Moving Bodies");` 应该返回 `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]` 。
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
2022-03-23 19:52:04 +05:30
JSON.stringify(remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies')) ===
2020-12-16 00:37:30 -07:00
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae',
'A Brief History of Time'
])
);
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
2021-11-06 08:56:52 -07:00
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
2021-01-13 03:31:00 +01:00
// Change code below this line
2022-01-11 22:27:22 +05:30
function add(bookName) {
2021-01-13 03:31:00 +01:00
bookList.push(bookName);
return bookList;
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
// Change code above this line
}
// Change code below this line
2022-01-11 22:27:22 +05:30
function remove(bookName) {
2021-11-06 08:56:52 -07:00
const book_index = bookList.indexOf(bookName);
2021-01-13 03:31:00 +01:00
if (book_index >= 0) {
bookList.splice(book_index, 1);
return bookList;
// 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
2021-11-06 08:56:52 -07:00
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
2021-01-13 03:31:00 +01:00
2021-11-06 08:56:52 -07:00
function add(bookList, bookName) {
2021-01-13 03:31:00 +01:00
return [...bookList, bookName];
}
2021-11-06 08:56:52 -07:00
function remove(bookList, bookName) {
2021-01-13 03:31:00 +01:00
const bookListCopy = [...bookList];
const bookNameIndex = bookList.indexOf(bookName);
if (bookNameIndex >= 0) {
bookListCopy.splice(bookNameIndex, 1);
}
return bookListCopy;
}
```