2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244c7
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 通过点号表示法访问对象属性
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/cGryJs8'
|
|
|
|
forumTopicId: 16164
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: accessing-object-properties-with-dot-notation
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
和访问数组类似,访问对象属性有两种方式:点号表示法(`.`)和方括号表示法(`[]`)。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
如果我们已经提前知道要访问的属性名,使用点号表示法是最方便的。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
这里是一个用点符号(`.`)读取对象属性的示例:
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const myObj = {
|
2020-04-29 10:29:13 +00:00
|
|
|
prop1: "val1",
|
|
|
|
prop2: "val2"
|
|
|
|
};
|
2021-10-27 15:10:57 +00:00
|
|
|
|
|
|
|
const prop1val = myObj.prop1;
|
|
|
|
const prop2val = myObj.prop2;
|
2020-04-29 10:29:13 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`prop1val` 的值将为字符串 `val1`,并且`prop2val` 的值将为字符串 `val2`。
|
2021-10-27 15:10:57 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
使用点号读取 `testObj` 的属性值。 将变量 `hatValue` 的值设置为该对象的 `hat` 属性的值,并将变量 `shirtValue` 的值设置为该对象的 `shirt` 属性的值。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`hatValue` 应该是一个字符串
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(typeof hatValue === 'string');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`hatValue` 的值应该为字符串 `ballcap`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(hatValue === 'ballcap');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`shirtValue` 应该是一个字符串
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(typeof shirtValue === 'string');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`shirtValue` 的值应该为字符串 `jersey`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(shirtValue === 'jersey');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你应该使用两个点号
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(code.match(/testObj\.\w+/g).length > 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-10-27 15:10:57 +00:00
|
|
|
const testObj = {
|
2021-01-13 02:31:00 +00:00
|
|
|
"hat": "ballcap",
|
|
|
|
"shirt": "jersey",
|
|
|
|
"shoes": "cleats"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
const hatValue = testObj; // Change this line
|
|
|
|
const shirtValue = testObj; // Change this line
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const testObj = {
|
2021-01-13 02:31:00 +00:00
|
|
|
"hat": "ballcap",
|
|
|
|
"shirt": "jersey",
|
|
|
|
"shoes": "cleats"
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:10:57 +00:00
|
|
|
const hatValue = testObj.hat;
|
|
|
|
const shirtValue = testObj.shirt;
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|