.
) and bracket notation ([]
), similar to an array.
Dot notation is what you use when you know the name of the property you're trying to access ahead of time.
Here is a sample of using dot notation (.
) to read an object's property:
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
testObj
using dot notation. Set the variable hatValue
equal to the object's property hat
and set the variable shirtValue
equal to the object's property shirt
.
hatValue
should be a string
testString: 'assert(typeof hatValue === ''string'' , ''hatValue
should be a string'');'
- text: The value of hatValue
should be "ballcap"
testString: 'assert(hatValue === ''ballcap'' , ''The value of hatValue
should be "ballcap"
'');'
- text: shirtValue
should be a string
testString: 'assert(typeof shirtValue === ''string'' , ''shirtValue
should be a string'');'
- text: The value of shirtValue
should be "jersey"
testString: 'assert(shirtValue === ''jersey'' , ''The value of shirtValue
should be "jersey"
'');'
- text: You should use dot notation twice
testString: 'assert(code.match(/testObj\.\w+/g).length > 1, ''You should use dot notation twice'');'
```