HTML and CSS Reference
In-Depth Information
EXAMPLE
8.2 (
CONTINUED
)
5
<script type = "text/javascript">
6
document.write("<b>The toy is a " +
toy.name
+ ".");
7
document.write("<br />It is a " +
toy.color
+""
+
toy.shape
+ ".");
8 </script>
</body>
</html>
EXPLANATION
1
JavaScript code starts here.
2
The
Object()
constructor is called with the
new
keyword to create an instance of
an object called
toy
. A reference to the new object is assigned to the variable,
toy
.
(All objects are descendants of the
Object o
bject.)
3
The
toy
object's
name
property is assigned
“Lego”
. The properties describe the
characteristics or attributes of the object. Properties are
not
variables. Do not use
the
var
keyword.
4
This is the end of the JavaScript program.
5
A new JavaScript program starts here in the body of the page.
6
The global object called
toy
is available within the script. The value of the
toy
ob-
ject's
name
property is displayed.
7
The values for the
color
and
shape
properties of the
toy
object are displayed.
8
This is the end of the JavaScript program. The output is shown in Figure 8.3.
Figure 8.3
The
toy
object and its properties.
In JavaScript you might see the syntax:
window.document.bgColor = "lightblue";
The
window
is the top object in the Browser Object Model. It is the parent of all par-
ents; the
document
is an object but, because it is subordinate to the
window
, it is also a
property of the
window
object. Although the background color,
bgColor
, is a property of
the document object, by itself it is not an object. (It is like an adjective because it
describes the document.)
window
document
bgColor