[Flashcoders] Question on variable init in a for loop
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 not resetted to null on every iteration as I would expect. I have to explicitly set the id to null myself! Does some know why this is, or are my expectations just wrong. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 test():void { var id:String; for each(var i:Object in arObj) { trace(1 ,id); id = test + Number(Math.random()).toString(); trace(2 ,id); } trace(i); } best a On 4 August 2010 10:45, Jiri jiriheitla...@googlemail.com wrote: 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 not resetted to null on every iteration as I would expect. I have to explicitly set the id to null myself! Does some know why this is, or are my expectations just wrong. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 a new scope int i = 8; } And you will get 2 different variables. Obviously, naming both the same is not the smartest move, but it's legal (at least in C#, if I recall correctly). Also when you write a loop, the counter var will be local if declared in the loop. I mean this: for(int i = 0; i 10; i++) { // do something with i } //this is invalid, i doesn not exist here... i = 20; Now, in Actionscript this is not possible. You can declare a variable wherever you like. The compiler ends up moving the variable to the top of the scope when generating the bytecode (I think this is called hoisting). So this: 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); } } Is equivalent to writting this: var l:Array = [{},{}] function test():void{ var id:String;// = null; for each(var i:Object in l){ trace(1 ,id); id = test + Math.random().toString(); trace(2 ,id); } } id will be declared (and initialized to null) only once, before you access it in your code. So you only have one variable, really and that's the reason for the behavior you observed. Cheers Juan Pablo Califano 2010/8/4 Jiri jiriheitla...@googlemail.com 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 not resetted to null on every iteration as I would expect. I have to explicitly set the id to null myself! Does some know why this is, or are my expectations just wrong. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Question on variable init in a for loop
Hope to add some value to this. The duplicate variable error is only due to the fact that String is a native object (don't know the correct name for this) of Flash AS3. So you don't have to use the new key word for it. Like this would work OK: var arObj:Array = [{},{}] function test():void { for each(var i:Object in arObj) { var id:MovieClip = new MovieClip() trace(1 ,id); id.name = test + Number(Math.random()).toString(); trace(2 ,id); } trace(i); } -Original Message- From: flashcoders-boun...@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 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 test():void { var id:String; for each(var i:Object in arObj) { trace(1 ,id); id = test + Number(Math.random()).toString(); trace(2 ,id); } trace(i); } best a On 4 August 2010 10:45, Jiri jiriheitla...@googlemail.com wrote: 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 not resetted to null on every iteration as I would expect. I have to explicitly set the id to null myself! Does some know why this is, or are my expectations just wrong. Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Geen virus gevonden in het binnenkomende-bericht. Gecontroleerd door AVG - www.avg.com Versie: 9.0.851 / Virusdatabase: 271.1.1/3049 - datum van uitgifte: 08/03/10 16:22:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 off and talk about private, protected, and public vars, even though it will lead us a bit OT. A private variable is available only to the class that declares it. A protected variable is available to that class and all derived classes. Of course, you can make any variable accessible with a getter or setter method. 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. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 default, internal is. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 static. A public variable, static or otherwise, is available to any caller. Technically, you are probably right, because a public variable isn't available until an object is intantiated from the class. A public static variable belongs to the class, not the instance, though, so it is available without instantiating an object. In fact, I use public static variables for my custom messages--I have a CustomMessage class specifically for that purpose. Nonetheless, once instantiated, and public variable is global whether it is static or not. That fits my concept of global. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 that the property variable is not global, because you have to explicitly call MyClass or a MyClass instance to access it. Now, some developers might imitate global variables in AS3 by doing something like making a class called Global, and accessing static variables such as Global.variable1, Global.variable2, etc. Taka On Wed, Aug 4, 2010 at 11:15 AM, Kerry Thompson al...@cyberiantiger.bizwrote: 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 static. A public variable, static or otherwise, is available to any caller. Technically, you are probably right, because a public variable isn't available until an object is intantiated from the class. A public static variable belongs to the class, not the instance, though, so it is available without instantiating an object. In fact, I use public static variables for my custom messages--I have a CustomMessage class specifically for that purpose. Nonetheless, once instantiated, and public variable is global whether it is static or not. That fits my concept of global. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 condition. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 for new devs in ECMAScript languages like AS3 and Javascript to grasp). So global really just refers to the top of the scope chain. Additionally, there are also as file level scopes. For example, in a file named getURL.as: package { function getURL() { trace(topScopeVar); } } var topScopeVar:String = Hello world!; That topScopeVar is only accessible from within that as file, because it is at the top of that file's individual scope chain. A function or class in a different as file would not have access to that variable, and this class has no access to other seemingly global scopes either, like the timeline, or other as files. functions and classes defined in root packages, and available in the class path, those are global, and you can define a property on those (static var on a class for example) - that's about as close as you get to a global in AS3. Kevin N. On 8/4/10 9:21 AM, 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. Other languages allow for creating further scopes. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 point--AS3 may not have true globals at all. I think he may be right, as long as we are using OOP. Consider this, though. Creat a new .fla, put a dynamic text field on stage, call it myText, then put this code in the first frame: var greeting:String = Hello. Am I global?; myText.text = greeting stop(); That's very AS2-ish code, but it works as an AS3 .fla. Is it a global? But, to your point about one single value per application, myObject.myPublicVar can have only one value. True, the class can be instantiated many times, but a non-static variable doesn't have a value until its class is instantiated. Even with these conditions, when you start using namespaces, you can have exactly the same variable in different name spaces. In that case, you could have myClass.myStaticPublicVar in two or more name spaces. That puts it in kind of the same category as public variables, doesn't it? A lot of us here came from Director, myself included, and we're used to Lingo's concept of a global namespace. I've programmed in many other languages--C, C++, COBOL, Forrtran, and others--and AS3 is the first I've used that doesn't really have a global namespace. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 Wed, Aug 4, 2010 at 2:30 PM, Kerry Thompson al...@cyberiantiger.bizwrote: 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 point--AS3 may not have true globals at all. I think he may be right, as long as we are using OOP. Consider this, though. Creat a new .fla, put a dynamic text field on stage, call it myText, then put this code in the first frame: var greeting:String = Hello. Am I global?; myText.text = greeting stop(); That's very AS2-ish code, but it works as an AS3 .fla. Is it a global? But, to your point about one single value per application, myObject.myPublicVar can have only one value. True, the class can be instantiated many times, but a non-static variable doesn't have a value until its class is instantiated. Even with these conditions, when you start using namespaces, you can have exactly the same variable in different name spaces. In that case, you could have myClass.myStaticPublicVar in two or more name spaces. That puts it in kind of the same category as public variables, doesn't it? A lot of us here came from Director, myself included, and we're used to Lingo's concept of a global namespace. I've programmed in many other languages--C, C++, COBOL, Forrtran, and others--and AS3 is the first I've used that doesn't really have a global namespace. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Question on variable init in a for loop
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 objects and other stuff. Thanks for your clarifications. Cheers Juan Pablo Califano 2010/8/4 Kevin Newman capta...@unfocus.com 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 for new devs in ECMAScript languages like AS3 and Javascript to grasp). So global really just refers to the top of the scope chain. Additionally, there are also as file level scopes. For example, in a file named getURL.as: package { function getURL() { trace(topScopeVar); } } var topScopeVar:String = Hello world!; That topScopeVar is only accessible from within that as file, because it is at the top of that file's individual scope chain. A function or class in a different as file would not have access to that variable, and this class has no access to other seemingly global scopes either, like the timeline, or other as files. functions and classes defined in root packages, and available in the class path, those are global, and you can define a property on those (static var on a class for example) - that's about as close as you get to a global in AS3. Kevin N. On 8/4/10 9:21 AM, 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. Other languages allow for creating further scopes. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders