--- id: 56533eb9ac21ba0edf2244c7 title: Accessing Object Properties with Dot Notation challengeType: 1 videoUrl: '' localeTitle: 使用点表示法访问对象属性 --- ## Description
有两种方法可以访问对象的属性:点表示法( . )和括号表示法( [] ),类似于数组。当您知道要提前访问的属性的名称时,使用点符号。以下是使用点表示法( . )读取对象属性的示例:
var myObj = {
prop1:“val1”,
prop2:“val2”
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
## Instructions
使用点表示法读入testObj的属性值。将变量hatValue设置为等于对象的属性hat ,并将变量shirtValue设置为等于对象的属性shirt
## Tests
```yml tests: - text: hatValue应该是一个字符串 testString: 'assert(typeof hatValue === "string" , "hatValue should be a string");' - text: hatValue的值应该是"ballcap" testString: 'assert(hatValue === "ballcap" , "The value of hatValue should be "ballcap"");' - text: shirtValue应该是一个字符串 testString: 'assert(typeof shirtValue === "string" , "shirtValue should be a string");' - text: shirtValue的值应该是"jersey" testString: 'assert(shirtValue === "jersey" , "The value of shirtValue should be "jersey"");' - text: 你应该使用点符号两次 testString: 'assert(code.match(/testObj\.\w+/g).length > 1, "You should use dot notation twice");' ```
## Challenge 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 ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```