Sign In/My Account | View Cart  
advertisement

Article:
 Introducing E4X
Subject: Array programming
Date: 2007-11-30 13:32:40
From: iconara

(the previous post, titled 'Arrr', was posted by me I must have accidentally pressed the wrong button, or the pirate in me wanted to have his say)


To me, the most important difference between working with plain javascript object structures (such as JSON) and working with XML using E4X is that the latter is an example of array programming.


Things like "html.body.p.a" aren't possible to express in most programming languages. The important thing is that in E4X that expression means something completely different than the same expression in the context of a regular JSON structure.


In E4X "html.body" returns a trampoline object (XMLList), which asks all the elements in it for their "p" children, returning yet another trampoline, which asks all it's elements for their "a" children. if the expression is "html.body.p.a.text()" the text method is called on all "a" nodes and a list of all the results is returned. When working with JSON, "html.body" returns one object, or a list of objects. If it's a list we have to write a loop to do something with the children. Just getting the text content of all "a" nodes would require three nested loops (depending on the actual data structure). Of course the code behing E4X uses loops, but the point is that we don't have to.


Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • Array programming
    2007-12-01 10:39:36 Kurt Cagle

    Re Arrr,


    This is one of the biggest difficulties I have in getting AJAX developers comfortable with e4x. As a language, e4x looks like JSON, but it acts more like a form of SQL, because you are essentially performing set manipulation rather than object manipulation. This is a not inconsiderable conceptual leap, and one that can even occasionally throw people that are comfortable working with XML.


    One way that I like to think about the distinction is that to object JavaScript, applying a list operation on a particular named property will generate an error. Applying the same operation on an E4X will be valid, because any named object exposes both the XML() and XMLList() interfaces - an object is a list of one element.


    I'm banking on XQuery becoming increasingly prevalent on that server, which should have as one immediate consequence the resurgence of XML as a messaging format to the client, which means that the ability to work with XML on the client will continue to climb. The tools that exist now - XPath in particular, have received only indifferent support in browsers (one of the big failings that I see in Firefox is that they don't wrap the awful, cumbersome W3C Xpath API in a more convenient package) and to a great extent works in a silo compared to the rest of JavaScript. E4X should help equalize that.





Sponsored By: