Monday, February 18, 2008

array.length vs xml.length() in as3

This is one of those things that annoys me, with ActionScript 3: the syntax for getting the length of an array is different from getting the length of a generation in XML.

When getting the number on elements in an array, you use length as a property of the array rather than a method. Such as

var max = myArray.length;

But to get this from an XML object, you have to add function brackets like this

var kids = myXML.length();

I'm guessing this is because automatically tracking the number of elements in an array is much simpler than for an XML object....certainly requires less overhead if the XML object is very complex.

Call me lazy, but I don't want to have to remember that length is a property of an array and a method of an XML object. Some consistency in syntax would be nice, even if inside it acts like a property sometimes and method at other times. It gets challenging to remember all this, especially if I'm switching between AS and other languages a lot.

1 comments:

Ben Stucki said...

I wondered the same thing when I noticed that. My guess was that it used a method because "length" could be a child node of the xml. Since parent.child is valid E4X syntax, it wouldn't really know if you wanted "length" as in a reference to the child node(s) or "length" as in the number of child nodes.