Funny things about static const in ActionScript 3
Friday 22 August 2008 - Filed under Tips
package
{
public class FunnyStaticConst
{
// mess around with the sequence but it still works
private static const num1:int = num2;
private static const num2:int = 100;
// a constant object?
private static const BASE:Object = {name:'banana', color:'yellow'};
// constructor
public function FunnyStaticConst()
{
//let's change the "constant"
BASE.name='apple';
BASE.color=red';
trace(BASE.name,BASE.color);
// the result is "apple,red"
}
}
}
2008-08-22 » admin

22 August 2008 @ 1:22 pm
It’s really funny !
Object is a dynamic class, so its properties can be added at run time. May be this situation happens to any other dynamic classes as well. Let’s try it on.
23 August 2008 @ 3:14 am
Actually, I think this makes a lot of sense.
FunnyStaticConst:BASE is a static constant, not it’s members, so of course you should be able to change them. You should not, however be allowed to redefine FunnyStaticConst:BASE altogether (such as FunnyStaticConst:BASE = {who:’me’, when:’now’}).
BTW: Either your wp spam blocker doesn’t like Safari, or my copy of Safari is jacked up.
23 August 2008 @ 4:33 am
I thought only numbers, strings etc can be a constant. After giving some more thoughts, I realize a constant object is a constant pointer pointing to something can be changed from time to time.
14 October 2008 @ 3:34 pm
[...] by wolsab on Tue 14-10-2008 org.as3s.Tween at AS3S.ORG Saved by saaipuntnl on Mon 13-10-2008 Funny things about static const in ActionScript 3 Saved by freckle73 on Mon 13-10-2008 ActionScriptCheatSheet.com – Adobe Flex, Adobe AIR, [...]
6 February 2009 @ 9:23 pm
i think it’s not funny, it should be that
8 April 2010 @ 6:24 am
If as3 is like most other languages, this is because any variable of a non-primitive (object) type only stores the reference to the actual object (i.e. the memory address of the object data). So the const is still constant because you cant set the BASE const to point to a different object, as you can with a normal var, such as:
var obj1:Object = {name:’banana’, color:’yellow’};
var obj2:Object = obj1;
obj2.name = ‘apple’;
trace(obj1.name); //output is apple