|
Straxus cat /dev/kbd | grep --random-lines > straxus.javadevelopersjournal.com | ||||||||||||||||||||||||||||||||||||||||||||||||||
Mailing List
Login Console
|
Actionscript classes, private vars, and unexpected static cling
Note: ActionScript is the Javascript-based language that Macromedia Flash uses. Macromedia Flex is an XML-based rapid application development platform - you create an .mxml page, and when a browser hits that page it is compiled by the server into a Flash movie (similar to how JSP and ASP work, except they [usually] compile to HTML). A FAQ is available if you would like more details. I want to warn you about a crazy ActionScript behaviour I recently encountered while working with Macromedia Flex. It appears that an ActionScript class is different enough from a normal class (in e.g. Java or C#) that bizarre things can result if you attempt to use it like a regular class. Take the following two example classes: class PrivateVarClassThese are two very similar (and very simple) classes, with one difference – PrivateVarClass declares _localArray as a new Array() in the var declaration, whereas ConstructorClass assigns a new Array() to _localArray in the constructor. Now, consider the following example code which was embedded in a Macromedia Flex <mx:Script> tag: var priv1 : PrivateVarClass = new PrivateVarClass(); var cons1 : ConstructorClass = new ConstructorClass();This code will create two parallel sets of classes, and do the same operations to both. It will output the contents of one of the object’s arrays at the end. However, when you run this code, you get the following output: priv1 Pages are 1:1,1:2,2:1,3:1,3:2,3:3, priv1 Pages length is 6 cons1 Pages are 1:1,1:2, cons1 Pages length is 2 This means that if you assign an Object to a private instance variable in the variable declaration, the single instance of that variable is shared by all instances of the class, as if it was declared static. This is very much unlike Java, C#, or any other language I’ve encountered to this point, so I advise that all variable assignment occur within the constructor, NOT in the variable declaration. | |||||||||||||||||||||||||||||||||||||||||||||||||