HTML and CSS Reference
In-Depth Information
EXAMPLE 8.8
<html>
<head><title>Object Literals</title>
<script type = "text/javascript">
1
var Car = { // Create a Car object
2
make:undefined,
year:2006,
price:undefined,
3
owner:{
name:"Henry Lee",
cell_phone: "222-222-2222",
4
address:{street: "10 Main",
city: "SF",
state: "CA"
}
},
dealer: "SF Honda",
5
display: function() {details="Make: "+Car.make+"\n";
details += "Year: "+Car.year+"\n";
details += "Price: $"+Car.price+"\n";
details += "Owner: "+
Car.owner.name+"\n";
alert(details);
}
};
</script>
</head>
<body>
<script type="text/javascript">
6
Car.make="Honda Civic" ; // Assign value
7
Car.year=2009 ; // Update the year
8
Car.price=30000 ;
9
Car.display() ;
</script>
</body>
</html>
EXPLANATION
1
An object literal Car is created and initialized.
2
The properties for the Car object are assigned. Properties are separated from their
corresponding values with a colon and each property/value pair is separated by a
comma, except the last one. Watch the commas. Internet Explorer is very partic-
ular about this rule.
3
The owner property contains another set of property/value pairs. The value of the
owner property is another nested object literal with key/value pairs enclosed in
curly braces.
Search WWH ::




Custom Search