HTML and CSS Reference
In-Depth Information
F
or...in
loops through the properties and methods of an object. The
variable
prop
is set to the label associated with the property or method.
You'll see it used most often in scripts that check to see if a browser
supports certain
HTML5
features. Note that the syntax
myObject
['myProperty']
is an alternative way of accessing the
myProperty
method.
This alternative approach is handy for use inside
for...in
loops.
Before we finish with objects, it's important to know that variables that
refer to objects behave slightly differently than variables that refer to
normal values like numbers. To see the difference, let's do a little
experiment in the console.
1
Create a variable
a
, and give it the
value
2
.
2
Create a variable
b
, and give it the
value of
a
.
3
Set the variable
b
to have a value of
4
instead.
Notice that after you do this, the value
of
a
is unchanged. Setting
b
to have a
value of
a
doesn't create any sort of
relationship between them. The value
contained in
a
is copied into
b
.
Now try a similar sequence of opera-
tions with two variables that refer to
objects. Notice that after assigning
myObject
as the value of
myOtherObject
,
changing the value of
myOtherObject
.myProperty
also changes the value of
myObject.myProperty
. This is because
assigning the object to another vari-
able creates two variables that refer to
the
same object
.