Quote:
First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];
|
Might be better to ask in what ways they are the same. To which the answer is simpler: In no way.
{ } is used to introduce a set of name/value pairs.
As in:
Code:
var sounds = {
dog : "bark",
cat : "meow",
cow : "moo",
"bald eagle" : "squawk!"
};
Note that you can put quotes around then names, but you don't have to unless they violate standard JavaScript naming conventions.
Also note that the above is entirely equivalent to doing:
Code:
var sounds = new Object();
sounds["dog"] = "bark"
sounds.cat = "meow";
sounds.cow = "moo";
sounds["bald eagle"] = "squawk!";
And all of this means that no matter which way you initialize the object, you can later find the value of on object element via
Code:
sounds.cat
or
sounds["cat"]
and so ont.
*************
The square bracket notation is used to create an array of elements (or to declare that a variable is an empty array, if there are no values between the brackets).
Code:
var arr = [ 7, 3, 21, 911 ];
var arr2 = [ "arrays", 3.14159265, "can be", new Date(), "heterogenous" ];
var arr3 = [ ]; // empty array
Array elements are then typically retrieved by element number:
Code:
alert( arr2[1] ); // shows pi
alert( arr2[3] ); // shows time and date when variable was created