简体   繁体   English

枚举类属性

[英]Enumerating class properties

I'm trying to iterate through the properties of a custom class, however, the methods provided by Adobe appear to not work. 我试图遍历自定义类的属性,但是, Adobe提供方法似乎不起作用。 I'm not receiving compile or run-time errors. 我没有收到编译或运行时错误。

Class

package {   
    public dynamic class enum {
        public var foo:Number = 123;

        public function enum() {
            this.setPropertyIsEnumerable("foo", true);
            if (this.propertyIsEnumerable("foo") == false) {
                trace("foo:" + foo + " is not enumerable.")
            }
        }
    }
}

// outputs "foo:bar is not enumerable."

Implementaiton Implementaiton

var test:enum = new enum();
for (var property:String in test) {
    trace(property);
}

// outputs nothing

I try to keep my code fast and flexible, so it's really frustrating when you must change the class to Dynamic just to be able to use for ... in on the properties. 我试图保持我的代码快速灵活,所以当您必须将类更改为Dynamic以便能够在属性上for ... in时,这确实令人沮丧。 Jackson Dunstan's testing confirms that this can be 400x slower than static class properties , but those have to be explicitly referenced (impractical for property agnostic methods), or use reflection of the class (computationally expensive) to be accessible. 杰克逊·邓斯坦(Jackson Dunstan)的测试证实,这可能比静态类的属性慢400倍 ,但是必须显式地引用这些属性 (对于不可知论的属性而言这是不切实际的),或者使用类的反射(在计算上昂贵)才能访问。

The only way I've found to sidestep the whole issue is to use dynamically declared variables... which is pointless since at that point using setPropertyIsEnumerable(prop, true) is superfluous; 我发现避开整个问题的唯一方法是使用动态声明的变量……这是没有意义的,因为在那一点上,使用setPropertyIsEnumerable(prop,true)是多余的; all dynamically created properties already are enumerable. 所有动态创建的属性均已枚举。 Additionally, dynamic variables cannot be strongly datatyped, and performance goes out the window. 此外,不能对动态变量进行严格的数据类型化,并且性能超出了预期。

For example... 例如...

Class

package {   
    public dynamic class enum {
        public var foo:String = "apple";

        public function enum(){
            this.dynamicVar = "orange";
            this.dynamicProp = "banana";
            this.setPropertyIsEnumerable("foo", true);
            this.setPropertyIsEnumerable("dynamicProp", false);
        }
    }
}

Implementation 履行

var test:enum = new enum();
for (var key:String in test) {
    trace(key + ": " + test[key]); // dynamicVar: 1
}

// outputs "dynamicVar: orange"

Now that the class is dynamic, we see that only one of our 3 test properties are being iterated. 现在,该类是动态的,我们看到只对3个测试属性之一进行了迭代。 There should be 2. 应该有2。

It almost feels like Adobe wants us to adopt bad programming habits. 几乎感觉像Adobe要我们养成不良的编程习惯。 Words fail me... 话让我失望...

Non-dynamic classes do not provide enumerable properties or methods. 非动态类不提供可枚举的属性或方法。

As stated in the description of the link you provided. 如您提供的链接说明中所述。

Sets the availability of a dynamic property for loop operations. 为循环操作设置动态属性的可用性。

I think you might want to refactor your code on this approach. 我认为您可能想在这种方法上重构代码。 I have never had to loop over a classes properties like you are doing here. 我从来没有像您在这里那样遍历类的属性。 If you want to track items dynamically you should use an associative array and track them that way not at the class level like you are doing. 如果要动态跟踪项目,则应使用关联数组并以这种方式跟踪它们,而不是像在做的那样在类级别进行跟踪。
And if you want strong data typing then use a vector. 如果要进行强数据键入,请使用向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM