According to the ActionScript 3.0 documentation, the TextFormat
classes size
property is an Object. Why is this? Clearly this is a number! When I check its type (using typeOf()
), it returns Number
. So whats the deal?
views:
139answers:
3No clue, just some ideas... quite a lot of properties are actually typed as Objects
when there's a possibility to pass various types (e.g String
in this case). This just makes it easier since you don't have to cast the type and convert it before applying the value. Also, note the initial value is null
(not NaN
as for a Number) . If text formats size is read when the value is null
the getter will return the default size. This way you can reset the value to default by simply setting it to null
.
Every object you use in ActionScript inherits from Object, which is why you can create an object with var newObject:Object
and assign it to be any type of existing object of a different type. Look in the docs at Number; it says at the top that its inheritance is from Object. It even says this for int, which is really considered a primitive.
I guess having that particular property return Object just abstracts the return type somewhat, not locking it in to an object further down the inheritance chain. If for some reason it has no number to return it can return null instead of undefined, like Theo.T said.
You don't really need to worry too much about it anyway, as you'll always get a Number out of it :)
The reason is that several primitive types are not nullable, including Number
(also Boolean
, int
and uint
). However null` means that the property has explicitely no value and shouldn't affect the according text property when applied to a text field. while in theory one could maybe use values as -1 or NaN, this is clearer.
I currently see it as my job to lure AS3 developers to switching to haXe :)
Thus, on a slightly unrelated matter, I'd like to point out that this is one of many weaknesses in AS3's type system. haXe defines the type of such value as Null<Float>
. If you do like strict typing, then you definitely should check out haXe.
greetz
back2dos