如何获取 Actionscript 中对象的属性列表?

发布于 2024-07-10 14:48:17 字数 303 浏览 7 评论 0原文

我有一个数据提供程序和一个分配给我的数据提供程序的数组的过滤函数。

当数据提供程序 (item.data) 传递给过滤器函数时,如何获取数据提供程序 (item.data) 每一行中的属性列表?

例如,如果我的对象包含:

  • 对象
    • 姓名
    • 电子邮件
    • 地址

然后我希望在我的过滤函数中能够查看姓名、电子邮件和地址。 不幸的是,我事先不知道这些属性是什么。

有任何想法吗?

I have a dataprovider and a filterfunction for my array that's assigned to my dataprovider.

How can I get a list of the properties that are in each row of the dataprovider (item.data) as it gets passed to the filterfunction?

For instance, if my object contained:

  • Object
    • name
    • email
    • address

Then I would want, in my filterfunction to be able to look at name, email and address. Unfortunately, I don't know what these properties will be before hand.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

若沐 2024-07-17 14:48:18

flash.utils .describeType(value:*) 还将为您提供对象的属性列表。

flash.utils.describeType(value:*) will also give you a list of properties on an object.

蓝咒 2024-07-17 14:48:18

for-in 仅适用于动态对象。 对于类型化对象,您需要使用某种反射来获取属性名称(例如 http: //www.as3commons.org/as3-commons-reflect/index.html

/安德烈。

for-in works for dynamic objects only. For typed objects you need to use some kind of reflection to get property names (e.g. http://www.as3commons.org/as3-commons-reflect/index.html)

/Andrei.

-柠檬树下少年和吉他 2024-07-17 14:48:18

您可能正在寻找

ObjectUtil.getClassInfo(object) 

,请参阅:

http ://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#getClassInfo%28%29

请注意,其中存在一个错误,导致它将 XML 视为非- 动态数据类型。
有关该错误的更多信息,请参见:
bugs.adobe.com/jira/browse/SDK-17712

You are probably looking for

ObjectUtil.getClassInfo(object) 

,see:

http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#getClassInfo%28%29

Be aware that there is a bug in it which causes it to treat XML as a non-dynamic data type.
More on the bug in:
bugs.adobe.com/jira/browse/SDK-17712

仙气飘飘 2024-07-17 14:48:18

对我来说只有这个有用:

trace('obj = '+getProperties(obj));

        public static function getProperties(obj:*):String  {
            var p:*;
            var res:String = '';
            var val:String;
            var prop:String;
            for (p in obj) {
                prop = String(p);
                if (prop && prop!=='' && prop!==' ') {
                    val = String(obj[p]);
                    if (val.length>10) val = val.substr(0,10)+'...';
                    res += prop+':'+val+', ';
                }
            }
            res = res.substr(0, res.length-2);
            return res;
        }

你会得到这样的东西:

obj = m:email@ra..., r:true

for me was useful only this:

trace('obj = '+getProperties(obj));

        public static function getProperties(obj:*):String  {
            var p:*;
            var res:String = '';
            var val:String;
            var prop:String;
            for (p in obj) {
                prop = String(p);
                if (prop && prop!=='' && prop!==' ') {
                    val = String(obj[p]);
                    if (val.length>10) val = val.substr(0,10)+'...';
                    res += prop+':'+val+', ';
                }
            }
            res = res.substr(0, res.length-2);
            return res;
        }

and you get something like this:

obj = m:email@ra..., r:true
独留℉清风醉 2024-07-17 14:48:18
// this method will work for retrieving properties of a *non-dynamic* (typed) object

// @return - all object properties
public function getProperties(_obj : *) : Array
{
        var _description : XML = describeType(_obj);
        var _properties : Array = new Array();
        for each (var prop:XML in _description.accessor)
        {
                var _property : Object = new Object();
                _property.name = String(prop.@name);
                _property.type = String(simple_type(prop.@type));
                _property.access = String(prop.@access);
                _property.declaredBy = String(prop.@declaredBy);
                try
                {
                   _property.value = _obj[_property.name];
                }
                catch (e : Error)
                {
                   _property.value = "";
                }
                _properties.push(_property)
        }
        _properties.sortOn("name");
        return _properties;
}

// better format for object class information
private function simple_type(_type : String) : String
{
        var lastIndex : int = _type.lastIndexOf("::");
        _type = lastIndex > 0 ? _type.substr(lastIndex + 2) : _type;
        return _type;
}
// this method will work for retrieving properties of a *non-dynamic* (typed) object

// @return - all object properties
public function getProperties(_obj : *) : Array
{
        var _description : XML = describeType(_obj);
        var _properties : Array = new Array();
        for each (var prop:XML in _description.accessor)
        {
                var _property : Object = new Object();
                _property.name = String(prop.@name);
                _property.type = String(simple_type(prop.@type));
                _property.access = String(prop.@access);
                _property.declaredBy = String(prop.@declaredBy);
                try
                {
                   _property.value = _obj[_property.name];
                }
                catch (e : Error)
                {
                   _property.value = "";
                }
                _properties.push(_property)
        }
        _properties.sortOn("name");
        return _properties;
}

// better format for object class information
private function simple_type(_type : String) : String
{
        var lastIndex : int = _type.lastIndexOf("::");
        _type = lastIndex > 0 ? _type.substr(lastIndex + 2) : _type;
        return _type;
}
楠木可依 2024-07-17 14:48:18

您可以使用 for .. in 循环来获取属性名称,或者使用 for every .. in 循环来获取属性值...


for( var o : * in object){
    trace( o + " = " + object[o] );
}
/************* OR ******************/
for each( var o : * in object ){
    trace( "object has property: " + o );
}

you can use a for .. in loop to get the properties names, or a for each .. in loop to get the property values ...


for( var o : * in object){
    trace( o + " = " + object[o] );
}
/************* OR ******************/
for each( var o : * in object ){
    trace( "object has property: " + o );
}
说好的呢 2024-07-17 14:48:18

这也会帮助你..
1. for 循环 - 将基于索引工作
2. 对于每个 - 递归调用直到长度
3. for in——用于读取属性值

     for( var obj : String in objectData ) //here objectData is your object
     {
        trace( "Object Name Is : " + obj );
        var data : Object = objectData[obj]; //here we get the object value by using the property name
        trace( "Value Is : " + data ); //Converts object to string
     }

This also will help you..
1. for Loop - Will work based on index
2. for each - recursive call upto the length
3. for in - used to read the property values

     for( var obj : String in objectData ) //here objectData is your object
     {
        trace( "Object Name Is : " + obj );
        var data : Object = objectData[obj]; //here we get the object value by using the property name
        trace( "Value Is : " + data ); //Converts object to string
     }
沉溺在你眼里的海 2024-07-17 14:48:17

如果它是一个动态对象,我相信你可以这样做:

var obj:Object; // I'm assuming this is your object

for(var id:String in obj) {
  var value:Object = obj[id];

  trace(id + " = " + value);
}

这就是 AS2 中的做法,并且我相信这仍然适用于 AS3 中的动态对象。 我认为它将显示的属性更多地局限于非动态对象。

If it's a dynamic object I believe you can just do something like this:

var obj:Object; // I'm assuming this is your object

for(var id:String in obj) {
  var value:Object = obj[id];

  trace(id + " = " + value);
}

That's how it's done in AS2, and I believe that still works for dynamic objects in AS3. I think the properties that it will show is more limited on non-dynamic objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文