简体   繁体   English

在AS3中动态调用属性

[英]Dynamically Invoke Property in AS3

I would like to dynamically invoke a Class 's Property via a String . 我想通过String动态调用ClassProperty In the following code, I can dynamically invoke a Class 's Function via a String . 在下面的代码中,我可以通过String动态调用ClassFunction

var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myStaticMethod"]();

where MyClass is defined as: 其中MyClass定义为:

package myPackage {
    public class MyClass {
         public function MyClass() {}
         public function myMethod():void {};
         public static function myStaticMethod():void {};
         public static function get myProperty():Object { return null; }
    }
}

However, a Property , such as MyClass.myProperty is not a Function . 但是,某个Property (如MyClass.myProperty )不是Function So, 所以,

var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myProperty"]();

throws an error: TypeError: Error #1006: value is not a function because myProperty is not a Function . 抛出错误: TypeError: Error #1006: value is not a function因为myProperty不是Function

Is there any way to do this dynamically via String s? 有没有办法通过String动态地做到这一点?

Thanks for the help. 谢谢您的帮助。

To solve this issue, I simply needed to remove the () from the code. 要解决这个问题,我只需要从代码中删除() That is, the new code looks like: 也就是说,新代码如下所示:

var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myProperty"]; // This works.

The Answer of Alex will indeed works properly, but only if you have the String written properly. Alex的答案确实可以正常工作,但前提是你正确编写了String。 Else you get this error thrown at you: TypeError: Error #1006: value is not a function. 否则你会收到这个错误: TypeError: Error #1006: value is not a function. To avoid this you could try test if the property or method is defined before using it. 为避免这种情况,您可以尝试在使用之前测试属性或方法是否已定义。 Like so: 像这样:

if(myClass["myProperty"] != undefined) 
{
...
}

Anyhow, in your specific example you are requesting a getter, and that's why you had to remove the () from your source. 无论如何,在您的具体示例中,您要求获取getter,这就是您必须从源中删除()的原因。 If you would be needing a method, I would also recommend you to save the method as a function: 如果您需要一个方法,我还建议您将该方法保存为函数:

var myFunction: Function = myClass["theFunction"];

And then to use either the call or the apply methods. 然后使用callapply方法。

myFunction.call(null, myParam);

IF you are interested in studying all the methods that an Object has and comparing them to a String. 如果您有兴趣研究Object的所有方法并将它们与String进行比较。 Consider also: 还要考虑:

var child:Sprite = new Sprite();
var description:XML = describeType(child);
var methodList: XMLList = description.descendants('method');

The attributes of a <method/> node are: <method/>节点的属性是:

  • name : The name of the method. name :方法的名称。
  • declaredBy : The class that contains the method definition. declaredBy :包含方法定义的类。
  • returnType : The data type of the method's return value. returnType :方法返回值的数据类型。

I hope this helps out, let me know if you found it useful. 我希望这会有所帮助,如果您发现它有用,请告诉我。

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

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