.
)和括号表示法( []
),类似于数组。当您知道要提前访问的属性的名称时,使用点符号。以下是使用点表示法( .
)读取对象属性的示例: var myObj = {
prop1:“val1”,
prop2:“val2”
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
testObj
的属性值。将变量hatValue
设置为等于对象的属性hat
,并将变量shirtValue
设置为等于对象的属性shirt
。 hatValue
应该是一个字符串
testString: assert(typeof hatValue === 'string' );
- text: hatValue
的值应该是"ballcap"
testString: assert(hatValue === 'ballcap' );
- text: shirtValue
应该是一个字符串
testString: assert(typeof shirtValue === 'string' );
- text: shirtValue
的值应该是"jersey"
testString: assert(shirtValue === 'jersey' );
- text: 你应该使用点符号两次
testString: assert(code.match(/testObj\.\w+/g).length > 1);
```