↑
Main Page
The for each..in Loop
As you can see, E4X represents a radical departure from traditional ECMAScript to support XML in a
simple yet powerful manner.
The for each..in Loop
Throughout the book, you have used the
for..in
loop to iterate over property names of an object. The
for each..in
loop, introduced in E4X, iterates the actual objects in an array. For example:
for each (var oItem in arrItems) {
alert(oItem);
}
To accomplish the same thing using a
for..in
loop, you use code that looks like this:
for (var sProperty in arrItems) {
alert(arrItems[sProperty]);
}
As you can see, the
for each..in
loop is a lot more useful and more like similar loops in other languages.
New classes
E4X introduces several new classes to deal specifically with XML:
?
Namespace
objects represent namespaces by using a URI and a prefix.
?
QName
objects represent XML qualified names composed of a local name and an optional
namespace URI.
?
XML
objects represent individual XML elements.
?
XMLList
objects contain any number of XML objects.
The Namespace class
Namespace
objects are a convenient way to reference namespaces in E4X. To create a
Namespace
, use its
constructor with one or two arguments:
var oNamespace1 = new Namespace(“http://www.wrox.com/”);
var oNamespace2 = new Namespace(“wrox”, “http://www.wrox.com/”);
In the first line, the constructor is called with just the URI of the namespace, which can be used when
dealing with XML like this:
<root xmlns=”http://www.wrox.com/”>
<message>Hello World!</message>
</root>
In the second line of the example, the constructor is being called with a namespace prefix and the URI,
which is useful when dealing with XML code that looks like this:
<wrox:root xmlns:wrox=”http://www.wrox.com/”>
<wrox:message>Hello World!</wrox:message>
</wrox:root>
607
The Evolution of JavaScript
23_579088 ch20.qxd 3/28/05 11:44 AM Page 607
Free JavaScript Editor
Ajax Editor
©
→