Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this this ActionScript syntax mean? ( Syntax for returning Objects Inline )

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.

like image 475
Tong Wang Avatar asked Jan 24 '23 23:01

Tong Wang


2 Answers

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.

like image 79
aaaidan Avatar answered Jan 26 '23 13:01

aaaidan


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.

like image 44
Luke Avatar answered Jan 26 '23 12:01

Luke