May 2008
Intermediate to advanced
172 pages
4h 54m
English
The for in statement allows for looping through
the names of all of the properties of an object. Unfortunately, it also loops
through all of the members that were inherited through the prototype chain. This has
the bad side effect of serving up method functions when the interest is in the data
members.
The body of every for in statement should be
wrapped in an if statement that does filtering.
if can select for a particular type or range
of values, it can exclude functions, or it can exclude properties from the
prototype. For example:
for (name in object) {
if (object.hasOwnProperty(name)) {
....
}
}