[Greasemonkey] e4x (say horray)

Aaron Boodman zboogs at gmail.com
Thu Sep 8 13:04:28 EDT 2005


E4X is one of the many improvements to JS that are sneaking into Deer
Park. E4X is a ridiculous idea, one that I'm sure language designers
around the world would cry themselves to sleep about if only Mozilla
would publicise it, and one of my new favorite toys in javascript.

Witness:

var x = <foo><bar>baz</bar></foo>;
alert(x.bar);
x.monkey = "good";
alert(x.toXMLString());

for each (var child in x) {
  alert(x.toXMLString());
}

x.bar += <bar id="1">hot</bar>;
x.bar += <bar id="2">dog</bar>;

for each (var bar in x.bar) {
  alert(bar. at id);
}

alert(x.bar(@id == 2));

You will be able to write code like this in Greasemonkey 0.6 with Deer Park.

There's very little documentation, the best thing I have found is to
read very slowly and carefully through the actual ECMA (E4X is
ratified!) spec -
http://www.ecma-international.org/publications/standards/Ecma-357.htm.
It's hard to read though because it's not meant for users of E4X but
for implementors. It took me a good couple of passes to really digest.

I can also imagine using this sorta the same way you'd use MochiKit's
DOM module (although this has all the problems of using innerHTML in
the first place):

var myDiv.innerHTML = <a href="{foo + '?id=' + bar}">{baz}</a>;

It's horrifically ugly, but it works and is sooooo efficient.


-- 
Aaron


More information about the Greasemonkey mailing list