English 中文(简体)
AS3 : 如何绕过此属性? 例如( var s in this)
原标题:AS3: How to loop through properties of this? Like for(var s in this)

类有方法从给定对象中更新其实例属性 。

出于某种原因, 在 ActionScript 3 中绕过 this 的属性失败 。

我试过一些东西,比如:

class myThing() {
    public var A:String;
    public var B:String;
    public var C:String;
    ...
    public function bindToObject( obj:Object){
        for( var s in this){
            if( obj.hasOwnProperty(s)) this[s] = obj[s];
        }
    }
}

这样循环就不会执行, 好像 this 没有任何属性一样 。

请以聪明的方式 将对象属性复制给我班级的一例

最佳回答

您不妨尝试换个角度来尝试。 意思是, 解析源对象属性, 并将其值指定给目标对象。 我本人也研究过这个问题, 下面是我是如何应用这个方法的:

private function parseData(data:Object):void
{
    for (var property:String in data)
        if (this.hasOwnProperty(property))
            this[property] = data[property];
}

它的用意与您的“ 坚固 > bindToobject < / 坚固” 方法非常相似, 但方法对参数对象进行剖析的情况除外。 选择此方法的原因, 是因为在我的上下文中, 目标对象的所有属性都被视为可选择的( 也就是说, 如果我只为源对象通过一个属性, 例如在3个对象中, 以3个对象中, 分析所有目标对象的属性不合理 ) 。 然而, 在您的情况中, 上下文可能有所不同 。

问题回答
class myThing() {
    public var A:String;
    public var B:String;
    public var C:String;
    ...

    setPropertyIsEnumerable("A");
    setPropertyIsEnumerable("B");
    setPropertyIsEnumerable("C");

假设您的类扩展名 Object





相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签