Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial50 Get data from an object
There are multiple ways to get properties of an object.
var scale = {
"name" : "C Major",
"pitches" : [60, 62, 64, 65, 67, 69, 71]
;
}
var name = scale.name;
var pitches = scale["pitches"];
var name2 = scale[0];
In the above example:
Lines 1–4 declare a simple object.
Line 6 is dot notation, literally the name of the property appended to the object’s name with a dot or period. Dot notation can be used as long as the key follows the rules for variable names.
Line 7 is the same as an array with square bracket notation. Keys consisting of multiple words in a
string
can be used here.Line 8 treats the object just like an array and uses the index for the first key to get the value.
If a key is used which is not actually in the object, then the object
will return undefined
.