Java Reference
In-Depth Information
■
Tip
the
index
in the for...in statement is of String type, not number type.
The
for..each..in
statement is not in the ECMAScript 5.1 specification. The
Mozilla JavaScript 1.6 extension that is supported by Nashorn. As the
for..in
statement
iterates over the indices/property names of a collection, the
for..each..in
statement
iterates over the values in the collection. It works in the same way as the
for-each
statement in Java. Notice that a set is a collection of unique values without giving names
to the values. You cannot iterate over a set using the
for..in
statement, but you can do so
using a
for..each..in
statement. Its syntax is:
for(var value in object)
Statement;
The following code shows how to use a
for..each..in
statement to iterate over the
elements (not indices) of an array:
// Create an array of three strings
var empNames = ["Ken", "Fred", "Li"];
// Use the for..each..in statement to iterate over elements of the array
for each(var empName in empNames) {
printf(empName);
}
Ken
Fred
Li
I will discuss more about the
for..in
and
for..each..in
statements in Chapter 7.
The continue, break, and return Statements
The
continue
,
break
, and
return
statements in Nashorn works the same as in Java. The
continue
and
break
statements can be labelled. The
continue
statement skips the rest of
the body of the iteration statement and jumps to the beginning of the iteration statement
to continue with the next iteration. The
break
statement jumps to the end of the iteration
and
switch
statement in which it appears. The
return
statement in a function returns the
control to the caller of the function. Optionally, the
return
statement can also return a
value to the caller.
Search WWH ::
Custom Search