Search code examples
actionscript-3redundancydynamic-typingweak-typing

What's the difference between Object, *, and no type at all?


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?


Solution

  • var x; and var 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 assign undefined to an Object will automatically cast it to null.

    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.