2020-08-04 07:13:35 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5cfa550e84205a357704ccb6
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 使用解构赋值来获取对象的值
|
2020-08-04 07:13:35 +00:00
|
|
|
|
challengeType: 1
|
|
|
|
|
forumTopicId: 301216
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: use-destructuring-assignment-to-extract-values-from-objects
|
2020-08-04 07:13:35 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
<dfn>解构赋值</dfn>是 ES6 引入的新语法,用来从数组和对象中提取值,并优雅地对变量进行赋值。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
有如下 ES5 代码:
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const user = { name: 'John Doe', age: 34 };
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
const name = user.name;
|
|
|
|
|
const age = user.age;
|
2020-08-04 07:13:35 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`name` 的值应该是字符串 `John Doe`, `age` 的值应该是数字 `34`。
|
|
|
|
|
|
|
|
|
|
下面是使用 ES6 解构赋值语句,实现相同效果:
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const { name, age } = user;
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
同样,`name` 的值应该是字符串 `John Doe`, `age` 的值应该是数字 `34`。
|
|
|
|
|
|
|
|
|
|
在这里,自动创建 `name` 和 `age` 变量,并将 `user` 对象相应属性的值赋值给它们。 这个方法简洁多了。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你可以从对象中提取尽可能多或很少的值。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
把两个赋值语句替换成效果相同的解构赋值语句。 `today` 和 `tomorrow` 的值应该还是 `HIGH_TEMPERATURES` 对象中 `today` 和 `tomorrow` 属性的值。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该移除 ES5 赋值语句。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
2021-02-06 04:42:36 +00:00
|
|
|
|
!__helpers
|
|
|
|
|
.removeJSComments(code)
|
|
|
|
|
.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
|
2020-12-16 07:37:30 +00:00
|
|
|
|
);
|
2020-08-04 07:13:35 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该使用解构赋值创建 `today` 变量。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(
|
2021-02-06 04:42:36 +00:00
|
|
|
|
__helpers
|
|
|
|
|
.removeJSComments(code)
|
|
|
|
|
.match(
|
|
|
|
|
/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
|
|
|
|
|
)
|
2020-12-16 07:37:30 +00:00
|
|
|
|
);
|
|
|
|
|
```
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该使用解构赋值创建 `tomorrow` 变量。
|
2020-08-04 07:13:35 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
2021-02-06 04:42:36 +00:00
|
|
|
|
__helpers
|
|
|
|
|
.removeJSComments(code)
|
|
|
|
|
.match(
|
|
|
|
|
/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
|
|
|
|
|
)
|
2020-12-16 07:37:30 +00:00
|
|
|
|
);
|
2020-08-04 07:13:35 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`today` 应该等于 `77`,`tomorrow` 应该等于 `80`。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(today === 77 && tomorrow === 80);
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const HIGH_TEMPERATURES = {
|
|
|
|
|
yesterday: 75,
|
|
|
|
|
today: 77,
|
|
|
|
|
tomorrow: 80
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
const today = HIGH_TEMPERATURES.today;
|
|
|
|
|
const tomorrow = HIGH_TEMPERATURES.tomorrow;
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
const HIGH_TEMPERATURES = {
|
|
|
|
|
yesterday: 75,
|
|
|
|
|
today: 77,
|
|
|
|
|
tomorrow: 80
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { today, tomorrow } = HIGH_TEMPERATURES;
|
|
|
|
|
```
|