Is there any difference between those three declarations?
var x;
var y:Object;
var z:*;
Is there anything in AS that's not an Object
?
Is there any difference between those three declarations?
var x;
var y:Object;
var z:*;
Is there anything in AS that's not an Object
?
In your code, x is not typed, y is typed as the Object class (the foundation class of all ActionScript classes) and z is typed as any type of class. The upshot of this is that anytime you need to reference the public members of those variables, you will either need to cast them as instances of the class you are looking to use or else (in the case of your y Object, but also any other un-typed variables) you will have to test if y.hasOwnProperty("propertyName")
before referencing it.
Usually you only see the * in method arguments that can take more than one type of class. For example, you might have an event handler like
private function myHandler(event:*) : void {
//statements
}
where event can refer to any type of event, and your method's code would determine which type that was before doing anything with it.
var x;
andvar x:*;
mean precisely the same thing to the compiler -- the variable can accept any type. Use:*
rather than omitting type to enhance readability of your code.Practically,
var x:Object;
is equivalent, since as you noted everything descends from Object. However, the compiler treats it different, and it tends to be slightly slower if you're accessing non-Object properties. Additionally, as noted by the other answers, attempting to assignundefined
to an Object will automatically cast it tonull
.I recommend using
:*
if your variable can accept more than one unrelated type of value, and using:Object
when using an Object as an associative array.