Its a little tricky to search for 'var:*' because most search engines wont find it.
I'm not clear exactly what var:* means, compared to say var:Object
I thought it would let me set arbitrary properties on an object like :
var x:* = myObject;
x.nonExistantProperty = "123";
but this gives me an error :
Property nonExistantProperty not found on x
What does * mean exactly?
Edit: I fixed the original var:* to the correct var x:*. Lost my internet connection
It means that the type is not specified and can be used with any type. However, you can't set random properties on it. It will behave like whatever type you set it to. The exact syntax is:
It's the "untyped" type. It just means that the variable can be of any type. Basically the same effect as using this:
It's a way of specifying an untyped variable so that you can basically assign any type to it. The code
creates the variable x then assigns the
oneTypeObject
variable to it. You can assign an entirely different type to it as well as follows:However, you still can't arbitrarily set or access properties; they have to exist in the underlying type (of either
oneTypeObject
oranotherTypeObject
).Both types may have identically named properties which means you can access or set that property in
x
without having to concern yourself with the underlying type.Expanding on the other answers, declaring something with type asterisk is exactly the same as leaving it untyped.
However, the question of whether you are allowed to assign non-existant properties to objects has nothing to do with the type of the reference, and is determined by whether or not the object is an instance of a dynamic class.
For example, since Object is dynamic and String is not:
Note how you can always assign new properties to the object, regardless of what kind of reference you use. Likewise, you can never assign new properties to the String, but if you use a typed reference then this will be caught by the compiler, and with an untyped reference the compiler doesn't know whether
b
is dynamic or not, so the error occurs at runtime.Incidentally, doc reference on type-asterisk can be found here:
(The markup engine refuses to linkify that, because of the asterisk.)
As they said before, it's untyped, so it may hold any kind of data. However, you cannot treat it as such in operations. For example, this is valid code:
But if you go one step farther, you have to watch out or you might get bitten:
Now, if the object returned by that function happens to be an object with a field with an id of "name," you are in the clear. However, unless you know for sure that this is the case, it's better to use typed objects. In this way, the compiler will alert you if you do something that might throw an error at runtime: