views:

1254

answers:

4

Hi,

I am a Java programmer and need to work on a Flex/ActionScript project right now. I got an example of using ITreeDataDesriptor from Flex 3 Cookbook, but there is one line of actionscript code that's hard for me to understand. I appreciate if someone could explain this a little further.

public function getData(node:Object, model:Object=null):Object
 {
  if (node is Office) {
   return {children:{label:node.name, label:node.address}};
  }
 }

The part that I didn't understand was "{children:{label:node.name, label:node.address}}". Office is simply a value object that contains two String properties: name and address.

+1  A: 

I think in Java you would call that a map or an associative array. In Javascript and Actionscript you can say this to create an object with certain properties:

var myobject = {
   'prop1': 100,
   'prop2': {
      'a': 1
   }
}

trace( myobject.prop1 );   // 100
trace( myobject.prop2.a );   // 1

In your example it's just returned as a nameless object.

Luke
+1  A: 
return {children:{label:node.name, label:node.address}};

Means you are returning a new Object. The {} are the Object's constructor, and in this case its an Anonymous object.

Robert Gould
A: 

Thank you both for the quick response. So if I understand your explanations correctly, the return statement is returning an anonymous object, and this object has only one property named "children", which is again an associative array - ok, here is the part I don't quite understand still, it seems that both properties in this array are named "label", is this allowed?

Tong Wang
That is odd, indeed
Robert Gould
That syntax is probably an ActionScript thing, it won't work as such in ECMA/Javascript. You'd need the "" marks to name your vars. Or it might be a typo in the book's code. Did you try compiling/looking at the included sample code(if there is any)?
Robert Gould
Although it's not invalid to declare a property twice in an anonymous object declaration, only the first one is used. So for the example in your question, the `label` property would have the value of `node.name`, and the second declaration would be ignored. Stupid, but whatever.
aaaidan
Tong Wang - Also, just a friendly pointer about responses to answers: they should generally be left as comments on the answer itself, rather than as a separate answer.
aaaidan
I tried the example and it worked fine. One thing I noticed is that the object that has "label" property defined twice is in getData() method, which doesn't seem to be called anywhere in Tree.as (the source of the Tree control), while the other 5 methods of the ITreeDataDescriptor are called.
Tong Wang
Does anyone know what is the purpose of getData()? The doc only says "Gets the data from a node.", which doesn't really help.
Tong Wang
+2  A: 

The following return expression (modified from the question) ...

return {children:{label:node.name, body:node.address}}

... is functionally equivalent to this code ...

var obj:Object = new Object();
obj.children = new Object();
obj.children.label = node.name;
obj.children.body = node.address;
return obj;

The anonymous object returned in the question code complicates matters because it defines a property twice. In that case, the first declaration is used, and the subsequent one(s) are ignored. No compile-time or runtime error is thrown.

aaaidan