2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b87367417b2b2512b42
|
|
|
|
challengeType: 1
|
2020-08-04 15:13:35 +08:00
|
|
|
forumTopicId: 301206
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 改变一个用 const 声明的数组
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:13:35 +08:00
|
|
|
<section id='description'>
|
|
|
|
在现代的 JavaScript 里,<code>const</code>声明有很多用法。
|
|
|
|
一些开发者倾向默认使用<code>const</code>来声明所有变量,但如果它们打算在后续的代码中修改某个值,那在声明的时候就会用<code>let</code>。
|
|
|
|
然而,你要注意,对象(包括数组和函数)在使用<code>const</code>声明的时候依然是可变的。使用<code>const</code>来声明只会保证它的标识不会被重新赋值。
|
|
|
|
|
|
|
|
```js
|
|
|
|
"use strict";
|
|
|
|
const s = [5, 6, 7];
|
|
|
|
s = [1, 2, 3]; // throws error, trying to assign a const
|
|
|
|
s[2] = 45; // works just as it would with an array declared with var or let
|
|
|
|
console.log(s); // returns [5, 6, 45]
|
|
|
|
```
|
|
|
|
|
|
|
|
从以上代码看出,你可以改变<code>[5, 6, 7]</code>自身,所以<code>s</code>变量指向了改变后的数组<code>[5, 6, 45]</code>。和所有数组一样,数组<code>s</code>中的数组元素是可以被改变的,但是因为使用了<code>const</code>关键字,你不能使用赋值操作符将变量标识<code>s</code>指向另外一个数组。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:13:35 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
这里有一个使用<code>const s = [5, 7, 2]</code>声明的数组。使用对各元素赋值的方法将数组改成<code>[2, 5, 7]</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
|
|
|
- text: 不要替换<code>const</code>关键字。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: getUserInput => assert(getUserInput('index').match(/const/g));
|
2020-08-04 15:13:35 +08:00
|
|
|
- text: <code>s</code>应该为常量 (通过使用<code>const</code>)。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: getUserInput => assert(getUserInput('index').match(/const\s+s/g));
|
2020-08-04 15:13:35 +08:00
|
|
|
- text: 不要改变原数组的声明。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: getUserInput => assert(getUserInput('index').match(/const\s+s\s*=\s*\[\s*5\s*,\s*7\s*,\s*2\s*\]\s*;?/g));
|
2020-08-04 15:13:35 +08:00
|
|
|
- text: <code>s</code>应该等于<code>[2, 5, 7]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(s, [2, 5, 7]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
const s = [5, 7, 2];
|
|
|
|
function editInPlace() {
|
2020-08-04 15:13:35 +08:00
|
|
|
'use strict';
|
2018-10-10 18:03:03 -04:00
|
|
|
// change code below this line
|
|
|
|
|
|
|
|
// s = [2, 5, 7]; <- this is invalid
|
|
|
|
|
|
|
|
// change code above this line
|
|
|
|
}
|
|
|
|
editInPlace();
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:13:35 +08:00
|
|
|
const s = [5, 7, 2];
|
|
|
|
function editInPlace() {
|
|
|
|
'use strict';
|
|
|
|
// change code below this line
|
|
|
|
|
|
|
|
// s = [2, 5, 7]; <- this is invalid
|
|
|
|
s[0] = 2;
|
|
|
|
s[1] = 5;
|
|
|
|
s[2] = 7;
|
|
|
|
// change code above this line
|
|
|
|
}
|
|
|
|
editInPlace();
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:13:35 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|