I have the following case:
var l:Array = [{},{}]
function test():void{
for each(var i:Object in l){
var id:String;// = null;
trace(1 ,id);
id = test + Math.random().toString();
trace(2 ,id);
}
}
I seems that 'id' is
no that's correct but id should be declared outside of your loop - as3 will
actually give you a duplicate variable error for putting it inside the loop.
you can also refer to your iterator (i) after the loop is done
i'd rewrite your function like this
var arObj:Array = [{},{}]
function
In actionscript you have only local and global scope. Meaning a variable
can be declared as local to the function or in the class that contains it.
Other languages allow for creating further scopes. In Java, C, C++, C#, for
instance, you can do something like this:
int i = 0;
{// this creates
...@chattyfig.figleaf.com
[mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of allandt
bik-elliott (thefieldcomic.com)
Sent: woensdag 4 augustus 2010 12:12
To: Flash Coders List
Subject: Re: [Flashcoders] Question on variable init in a for loop
no that's correct but id should be declared outside
Juan Pablo Califano wrote:
In actionscript you have only local and global scope. Meaning a variable
can be declared as local to the function or in the class that contains it.
That's true, and relevant to the OP's problem. Juan Pablo is rignt on
the money, so I'm going to pick up where he left
Kerry Thompson wrote:
In OOP AS3, only public variables are truly global. If you don't
declare a variable to be private or protected, it is global by
default.
Public static properties are the only thing that can be compared with a
global. Note the word static.
And public is not the
Henrik Andersson wrote:
Public static properties are the only thing that can be compared with a
global. Note the word static.
And public is not the default, internal is.
You're right, Henrick. Internal is the default, not public. My bad.
I think we're splitting hairs on public vs. public
Public variables, static or otherwise do NOT classify as global variables.
http://en.wikipedia.org/wiki/Global_variable
A global variable is something that is accessible everywhere without any
scope whatsoever.
By having to call MyClass.variable or myClassInstance.variable, it is quite
apparent
Kerry Thompson wrote:
Nonetheless, once instantiated, and public variable is global whether
it is static or not. That fits my concept of global.
My definition of a global variable is:
* One single value per application
* Can be accessed by any code
A public property only satisfies the second
That's an over simplification of the scope chain properties of AS3,
and global isn't the right word there (though I know what you meant).
To clarify the scope chain issue, keep in mind that you can nest
function scopes to create a hierarchy of scopes with a closure (usually
this is difficult
Henrik Andersson wrote:
My definition of a global variable is:
* One single value per application
* Can be accessed by any code
A public property only satisfies the second condition.
Again, I think we're talking about a purely semantic difference, not a
functional one. Actually, Taka has a
It's not a global because if you have a MovieClip called testMC on the stage
and are inside testMC and try to do trace(greeting), you will get an error.
To access that property you have to do parent.greeting, therefore because
you have to be aware of the scope to access it, it is not global.
On
Agreed, that was over simplified.
I probably should have said clearly I was using global in a rather loose
way -- as opposed to function local; that's why I put the quotes. I just
wanted to point out what the problem was in the original question, in a
simple way, without getting into activation
13 matches
Mail list logo