HTML and CSS Reference
In-Depth Information
FORMAT
var object = { property1: value, property2: value };
EXAMPLE
var area = { length: 15, width: 5 };
In the next example, two methods are created for the object literal. Once an object
literal has been defined you can later add more properties to it as demonstrated in
Example 8.7.
EXAMPLE 8.7
<html>
<head><title>working with literal objects</title>
<script type="text/javascript">
1 var soldier ={
2 name: undefined,
3 rank: "captain",
4 picture: "keeweeboy.jpg",
5 fallIn: function() {
alert("At attention, arms at the side, head and
eyes forward.");
},
6 fallOut: function() {
alert("Drop out of formation, step back, about face!");
}
};
</script>
</head>
<body>
<big>
<script type="text/javascript">
7 soldier.name="Tina Savage"; // Assign value to object
// property
8 document.write("The soldier's name is ",
soldier.name ,".<br />");
document.write(soldier.name+"'s rank is ",
soldier.rank+ ."<br />”);
9 document.write("<img src='"+soldier.picture+"'>")
10 soldier.fallIn() ; //call object's methods
11 soldier.fallOut() ;
</script>
</big>
</body>
</html>
Search WWH ::




Custom Search