HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The
Object()
constructor creates and returns a reference to a
cat
object.
2
The returned value from the
Object()
constructor is a reference to an object, as
shown in the Figure 8.2.
[object Object]
means that the cat is a descendant of the
Object
object from which all objects are derived.
Figure 8.2
cat
is an object.
Properties describe the object. The object name is followed by a dot and a property. The
properties are accessible only via their object. In Figure 8.3, the top object is the
Object
object. If we create a new
cat
and a
dog
object, as shown in Example 8.2, they are descen-
dants of the
Object
object. Although the
dog
and the
cat
are objects in their own right,
they are considered properties of the
Object
object. In fact, any object subordinate to
another object is also a property of that object. The
cat
and the
dog
are descendants of
the
Object
object. They also have properties that describe them, such as
name
,
color
,
size
,
and so forth. The
Object
object is not listed in the hierarchy when using the dot syntax.
To assign properties to the
cat
object, the syntax would be as follows:
cat.name = "Sneaky";
cat.color="yellow";
In Example 8.2 a new object is created with the
new
keyword and the
Object()
construc-
tor. Properties are assigned to the object and their values retrieved.
EXAMPLE
8.2
<html>
<head><title>User-defined objects</title>
1
<script type = "text/javascript">
2
var toy = new Object();
// Create an instance of the object
3
toy.name = "Lego";
// Assign properties to the object
toy.color = "red";
toy.shape = "rectangle";
4
</script>
</head>
<body bgcolor="lightblue">