2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b7c367417b2b2512b1a
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 使用方括号访问属性名称
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-08-04 15:14:21 +08:00
|
|
|
forumTopicId: 301150
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: access-property-names-with-bracket-notation
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
在关于对象的第一个挑战中,我们提到可以在一对方括号中用一个变量作为属性名来访问属性的值。假设一个超市收银台程序中有一个 `foods` 对象,并且有一个函数会设置 `selectedFood`;如果我们需要查询 `foods` 对象中,某种食物是否存在,可以这样实现:
|
2020-08-04 15:14:21 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
let selectedFood = getCurrentFood(scannedItem);
|
|
|
|
let inventory = foods[selectedFood];
|
|
|
|
```
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
上述代码会先读取 `selectedFood` 变量的值,并返回 `foods` 对象中以该值命名的属性所对应的属性值。若没有以该值命名的属性,则会返回 `undefined`。有时候对象的属性名在运行之前是不确定的,或者我们需要动态地访问对象的属性值。在这些场景下,方括号表示法就变得十分有用。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
我们已经定义了 `checkInventory` 函数,它接受一个被扫描到的商品名作为输入参数。请让这个函数返回 `foods` 对象中,以 `scannedItem` 的值所命名的属性对应的属性值。在本挑战中,只有合理有效的属性名会作为参数传入 `checkInventory`,因此你不需要处理参数无效的情况。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`checkInventory` 应是一个函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.strictEqual(typeof checkInventory, 'function');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`foods` 对象应只包含以下键值对:`apples: 25`、`oranges: 32`、`plums: 28`、`bananas: 13`、`grapes: 35`、`strawberries: 27`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert.deepEqual(foods, {
|
2018-10-10 18:03:03 -04:00
|
|
|
apples: 25,
|
|
|
|
oranges: 32,
|
|
|
|
plums: 28,
|
|
|
|
bananas: 13,
|
|
|
|
grapes: 35,
|
|
|
|
strawberries: 27
|
2020-12-16 00:37:30 -07:00
|
|
|
});
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`checkInventory("apples")` 应返回 `25`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.strictEqual(checkInventory('apples'), 25);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`checkInventory("bananas")` 应返回 `13`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.strictEqual(checkInventory('bananas'), 13);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`checkInventory("strawberries")` 应返回 `27`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert.strictEqual(checkInventory('strawberries'), 27);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:21 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
let foods = {
|
|
|
|
apples: 25,
|
|
|
|
oranges: 32,
|
|
|
|
plums: 28,
|
|
|
|
bananas: 13,
|
|
|
|
grapes: 35,
|
|
|
|
strawberries: 27
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkInventory(scannedItem) {
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(checkInventory("apples"));
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
let foods = {
|
|
|
|
apples: 25,
|
|
|
|
oranges: 32,
|
|
|
|
plums: 28,
|
|
|
|
bananas: 13,
|
|
|
|
grapes: 35,
|
|
|
|
strawberries: 27
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkInventory(scannedItem) {
|
|
|
|
return foods[scannedItem];
|
|
|
|
}
|
|
|
|
```
|