Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial49 Create an Object
One way to declare an object is assigning the variable with a pair of
empty braces { }
, otherwise known as an object
literal. This creates an object that is considered empty, meaning
it has no key-value pairs:
var scale = {};
Another way to declare an object is assigning the variable with data between the braces:
var scale = {
"name" : "C Major",
"pitches" : [60, 62, 64, 65, 67, 69, 71]
; }
In the above example:
Line 1: The variable is declared with the opening brace.
Line 2: The first key-value pair is declared. Similar to declaring a variable, a key is created with the string
"name"
and the value for that key is the string"C Major"
. The key and value are separated with the colon:
.Line 3: The second key-value pair is declared. It is separated from the first pair with a comma, just like in an array.
Line 4: The closing brace is used with the semicolon to end the statement which declares the variable.
The third way to declare an object is to use the new
keyword otherwise known as an object constructor.
var scale = new Object();