Review of Object Literals

Contact Us or call 1-877-932-8228
Review of Object Literals

Review of Object Literals

Arrays

Array literals are created with square brackets as shown below:

var beatles = ["Paul","John","George","Ringo"];

This is the equivalent of:

var beatles = new Array("Paul","John","George","Ringo");

Objects

Object literals are created with curly brackets:

var beatles = {
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll"
}

This is the equivalent of:

var beatles = new Object();
beatles.Country = "England";
beatles.YearFormed = 1959;
beatles.Style = "Rock'n'Roll";

Just as with all objects in JavaScript, the properties can be references using dot notation or bracket notation.

alert(beatles.Style); //Dot Notation
alert(beatles["Style"]); //Bracket Notation

Arrays in Objects

Object literals can contain array literals:

var beatles = {
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll",
"Members" : ["Paul","John","George","Ringo"]
}

Objects in Arrays

Array literals can contain object literals:

var rockbands = [
	{
	"Name" : "Beatles",
	"Country" : "England",
	"YearFormed" : 1959,
	"Style" : "Rock'n'Roll",
	"Members" : ["Paul","John","George","Ringo"]
},
{
	"Name" : "Rolling Stones",
	"Country" : "England",
	"YearFormed" : 1962,
	"Style" : "Rock'n'Roll",
	"Members" : ["Mick","Keith","Charlie","Bill"]
}
]
Next