Availability: |
| ||||||||
JavaScript syntax: | - | aLabel: for(anLValue in anObject) { someCode }; | |||||||
Argument list: | aLabel | An optional label to identify the loop | |||||||
anLValue | A value that can be assigned to | ||||||||
anObject | An object whose properties will be cycled | ||||||||
someCode | Some code that is executed each time round the loop |
A for... in iteration statement is used to enumerate through the properties of an object.
The first item in the control construct is a container that a value can be assigned to. An LValue or Left-Hand Side expression in other words. The item following the in keyword, is the object whose properties are to be enumerated.
Each time round the loop, the name of an object property will be assigned to the LValue and it can then be used as an index to the property value in that object.
Properties that have the DontEnum attribute set will not be enumerated in this iteration statement.
During each iteration, the property name can be used as an array index key to extract the property value.
At version 1.2 of JavaScript, a named continue can be used with this iterator. If the named continue is executed, control passes to the top of the named for loop. The loop starts over with the next property name being assigned to the specified variable.
It is often the case that some properties will not be enumerated with this mechanism. In particular, properties of host objects seem particularly prone to this problem.
// Loop through the properties of an object only printing properties // that have the string data type. for(myProperty in myObject) { if(typeof(myObject[myProperty]) == "string") { document.write(myProperty, myObject[myProperty]); } }
ECMA 262 edition 2 - section - 11.1.2
ECMA 262 edition 2 - section - 12.6.3
ECMA 262 edition 2 - section - 12.7
ECMA 262 edition 2 - section - 12.8
ECMA 262 edition 3 - section - 12.6.4
ECMA 262 edition 3 - section - 12.7
ECMA 262 edition 3 - section - 12.8
Wrox Instant JavaScript - page - 34
Prev | Home | Next |
for( ... ) ... | Up | Form |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |