Main Page

The for Loop

As you can see, E4X represents a radical departure from traditional ECMAScript to support XML in a
simple yet powerful manner.
The for Loop
Throughout the book, you have used the
loop to iterate over property names of an object. The
loop, introduced in E4X, iterates the actual objects in an array. For example:
for each (var oItem in arrItems) {
To accomplish the same thing using a
loop, you use code that looks like this:
for (var sProperty in arrItems) {
As you can see, the
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:
objects represent namespaces by using a URI and a prefix.
objects represent XML qualified names composed of a local name and an optional
namespace URI.
objects represent individual XML elements.
objects contain any number of XML objects.
The Namespace class
objects are a convenient way to reference namespaces in E4X. To create a
, use its
constructor with one or two arguments:
var oNamespace1 = new Namespace(“”);
var oNamespace2 = new Namespace(“wrox”, “”);
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=””>
<message>Hello World!</message>
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=””>
<wrox:message>Hello World!</wrox:message>
The Evolution of JavaScript
23_579088 ch20.qxd 3/28/05 11:44 AM Page 607

JavaScript EditorFree JavaScript Editor     Ajax Editor