2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244c9
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cnQyKur'
|
|
|
|
|
forumTopicId: 16165
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 通过变量访问对象属性
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
中括号操作符的另一个使用方式是访问赋值给变量的属性。当你需要遍历对象的属性列表或访问查找表(lookup tables)时,这种方式极为有用。
|
|
|
|
|
这有一个使用变量来访问属性的例子:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var dogs = {
|
|
|
|
|
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
|
|
|
|
|
};
|
|
|
|
|
var myDog = "Hunter";
|
|
|
|
|
var myBreed = dogs[myDog];
|
|
|
|
|
console.log(myBreed); // "Doberman"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
使用此概念的另一种方法是在程序执行期间动态收集属性名称,如下所示:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var someObj = {
|
|
|
|
|
propName: "John"
|
|
|
|
|
};
|
|
|
|
|
function propPrefix(str) {
|
|
|
|
|
var s = "prop";
|
|
|
|
|
return s + str;
|
|
|
|
|
}
|
|
|
|
|
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
|
|
|
|
|
console.log(someObj[someProp]); // "John"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
提示:当我们通过变量名访问属性的时候,不需要给变量名包裹引号。因为实际上我们使用的是变量的值,而不是变量的名称。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
使用变量<code>playerNumber</code>,通过中括号操作符找到<code>testObj</code>中<code>playerNumber</code>为<code>16</code>的值。然后把名字赋给变量<code>player</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>playerNumber</code>应该是一个数字。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof playerNumber === 'number');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 变量<code>player</code>应该是一个字符串。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof player === 'string');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>player</code>点值应该是 "Montana"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(player === 'Montana');
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该使用中括号访问<code>testObj</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/testObj\s*?\[.*?\]/.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你不应该直接将<code>Montana</code>赋给<code>player</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该在中括号中使用<code>playerNumber</code>变量。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
|
|
|
|
var testObj = {
|
|
|
|
|
12: "Namath",
|
|
|
|
|
16: "Montana",
|
|
|
|
|
19: "Unitas"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Only change code below this line;
|
|
|
|
|
|
|
|
|
|
var playerNumber; // Change this Line
|
|
|
|
|
var player = testObj; // Change this Line
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
if(typeof player !== "undefined"){(function(v){return v;})(player);}
|
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 = {
|
|
|
|
|
12: "Namath",
|
|
|
|
|
16: "Montana",
|
|
|
|
|
19: "Unitas"
|
|
|
|
|
};
|
|
|
|
|
var playerNumber = 16;
|
|
|
|
|
var player = testObj[playerNumber];
|
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>
|