English 中文(简体)
How to dynamically generate variables in Action Script 2.0
原标题:

I have a for loop in action script which I m trying to use to dynamically create variable.

Example

for( i = 0 ; i &lt 3 ; i++)
{
    var MyVar+i = i;
}

after this for loop runs, i would like to have 3 variables named MyVar1, MyVar2, MyVar3. I know the code above will give you a syntax error, but that is just to illustrate what I am trying to do. Any takers?

The primary reason i m doing this is because I m having scope problems noted here in this other unanswered Action Script question: How to pass variables into inline functions in Action Script 2

Thanks!

最佳回答

I could be wrong (I haven t done AS2 for a long while), but I think you can do this using array syntax:

 for( i = 0 ; i < 3 ; i++)
{
    this["myVar"+i] = i;
}

and then for variable access:

var foo = this["myVar0"] //etc
问题回答

First answer is correct, but if you make the class dynamic (ie. new members can be created dynamically) ...

dynamic class ClassName { // etc. }

... then you can reference the variable in normal syntax:

var foo = this.myVar0;

You won t be able to access the variable at all without this whether the class is dynamic or not.





相关问题
Flex: Text Input that accepts number only

Need a code that only accepts numbers. Upon inputting, the code must check if it is number, if not, it must remove the entered key or not enter it at all

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I m trying to use to dynamically create variable. Example for( i = 0 ; i &lt 3 ; i++) { var MyVar+i = i; } after this for loop runs, i would like to ...

drag file(s) from destop directly to flash webpage

Could someone please let me know if is possible to drag (multiple) files from desktop directly into a flash webpage. If yes could you please link me to some online resources.

Can XMLSocket send more than once in a frame?

I have a XMLSocket and I call send twice in the same function. The first send works but the second does not? Does XMLSocket have a restriction to only send one message per frame? Do I have to queue ...

How do you stop a setInterval function?

I have a function that I want to run at an interval within a frame. I use the following code to start the function: var intervalID = setInterval(intervalFunction, 3000); Then, in a button s ...

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 =...

热门标签