2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244c7
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cGryJs8'
|
|
|
|
forumTopicId: 16164
|
|
|
|
localeTitle: 通过点符号访问对象属性
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
有两种方式访问对象属性,一个是点操作符(<code>.</code>),一个是中括号操作符(<code>[]</code>)。
|
|
|
|
当你知道所要读取的属性的名称的时候,使用点操作符。
|
|
|
|
这是一个使用点操作符读取对象属性的例子:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var myObj = {
|
|
|
|
prop1: "val1",
|
|
|
|
prop2: "val2"
|
|
|
|
};
|
|
|
|
var prop1val = myObj.prop1; // val1
|
|
|
|
var prop2val = myObj.prop2; // val2
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
通过点操作符读取对象<code>testObj</code>,把<code>hat</code>的属性值赋给变量<code>hatValue</code>,把<code>shirt</code>的属性值赋给<code>shirtValue</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>hatValue</code>应该是一个字符串。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof hatValue === 'string' );
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>hatValue</code>的值应该是<code>"ballcap"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(hatValue === 'ballcap' );
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>shirtValue</code>应该是一个字符串。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof shirtValue === 'string' );
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>shirtValue</code>的值应该是<code>"jersey"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(shirtValue === 'jersey' );
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 你应该使用点操作符两次。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/testObj\.\w+/g).length > 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var testObj = {
|
|
|
|
"hat": "ballcap",
|
|
|
|
"shirt": "jersey",
|
|
|
|
"shoes": "cleats"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
var hatValue = testObj; // Change this line
|
|
|
|
var shirtValue = testObj; // Change this line
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
var testObj = {
|
|
|
|
"hat": "ballcap",
|
|
|
|
"shirt": "jersey",
|
|
|
|
"shoes": "cleats"
|
|
|
|
};
|
|
|
|
|
|
|
|
var hatValue = testObj.hat;
|
|
|
|
var shirtValue = testObj.shirt;
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|