这个ActionScript语法是什么意思? (返回对象内联的语法)

时间:2023-01-09 22:29:41

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.

我是一名Java程序员,现在需要处理Flex / ActionScript项目。我得到了一个使用Flex 3 Cookbook的ITreeDataDesriptor的例子,但有一行动作脚本代码很难理解。如果有人能够进一步解释这一点我感激不尽。

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.

我不理解的部分是“{children:{label:node.name,label:node.address}}”。 Office只是一个包含两个String属性的值对象:名称和地址。

4 个解决方案

#1


7  

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.

问题代码中返回的匿名对象使问题复杂化,因为它定义了两次属性。在这种情况下,使用第一个声明,并忽略后续声明。不会抛出编译时或运行时错误。

#2


1  

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:

我认为在Java中你会称之为地图或关联数组。在Javascript和Actionscript中,您可以这样说来创建具有某些属性的对象:

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.

在您的示例中,它只是作为无名对象返回。

#3


1  

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.

意味着您要返回一个新对象。 {}是Object的构造函数,在本例中是一个Anonymous对象。

#4


0  

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?

谢谢你们的快速反应。因此,如果我正确理解你的解释,return语句将返回一个匿名对象,并且这个对象只有一个名为“children”的属性,这又是一个关联数组 - 好的,这是我还不太了解的部分,似乎这个数组中的两个属性都被命名为“label”,这是允许的吗?

#1


7  

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.

问题代码中返回的匿名对象使问题复杂化,因为它定义了两次属性。在这种情况下,使用第一个声明,并忽略后续声明。不会抛出编译时或运行时错误。

#2


1  

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:

我认为在Java中你会称之为地图或关联数组。在Javascript和Actionscript中,您可以这样说来创建具有某些属性的对象:

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.

在您的示例中,它只是作为无名对象返回。

#3


1  

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.

意味着您要返回一个新对象。 {}是Object的构造函数,在本例中是一个Anonymous对象。

#4


0  

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?

谢谢你们的快速反应。因此,如果我正确理解你的解释,return语句将返回一个匿名对象,并且这个对象只有一个名为“children”的属性,这又是一个关联数组 - 好的,这是我还不太了解的部分,似乎这个数组中的两个属性都被命名为“label”,这是允许的吗?