简体   繁体   English

变量的AS3对象表示法

[英]AS3 object notation for variable

I have some variables in the format: 我有一些格式的变量:

var like6Y:Number = 50;

Later I am dynamically setting some: 后来我动态设置一些:

num=6;

Then I am trying to use that num to get the variable like6Y: 然后我试图使用该数字来获取变量like6Y:

 like+6+Y

I know that is totally wrong but I seem to remember you could use some kind of object notation to accomplish this: 我知道这是完全错误的,但我似乎记得你可以使用某种对象符号来完成这个:

["like"+num+"Y"] 

Or something along those lines. 或类似的规定。 But I can't remember how to do it. 但我不记得怎么做了。 Thanks 谢谢

It can be done - just access this["like"+num+"Y"] . 它可以完成 - 只需访问this["like"+num+"Y"] Assuming like6num is a property of the current object, otherwise just change this to the object's name. 假设like6num是当前对象的属性,否则只需要改变this对象的名字。

Even though something like this["like" + num + "Y"] would work for global variables, the datatypes Array and Vector (use Vector if you know all values will be of the same type and that every index of it's length will be assigned a value) are built for the task of accessing a value by an integer reference. 即使像this["like" + num + "Y"]这样的东西适用于全局变量,数据类型为ArrayVector (如果你知道所有值都是相同的类型,那么使用Vector,并且它的每个索引的长度都是分配的值)专门为一个整数参考访问的值的任务。 They also provide the added benefit of giving you greater control of the scope in which the variables are placed. 它们还提供了额外的好处,使您可以更好地控制变量的放置范围。

For instance: 例如:

var likeY:Array = [-10, 0, 10, 20, 30, 40, 50];
var num:int = 6;
trace(likeY[num]); //prints 50

in Javascript you can use window["like"+num+"Y"] or eval("like"+num+"Y") ... but there is no alternative in AS3. 在Javascript中你可以使用window["like"+num+"Y"]eval("like"+num+"Y") ...但是在AS3中没有其他选择。

Also this["like"+num+"Y"] work if you are trying to access a public *property of the current class. 如果您尝试访问当前类的public *属性, this["like"+num+"Y"]工作。

But there are some third party libs which brings the eval method to AS3. 但是有一些第三方库将eval方法引入AS3。 I never used it, but you can try, [Eval in AS3: Tips for Executing Dynamic Actionscript][1] http://danielmclaren.com/node/113 我从来没用过它,但是你可以尝试,[在AS3中的Eval:执行动态ActionScript的提示] [1] http://danielmclaren.com/node/113

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

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