What exactly does var x:* mean in actionscript?

2019-06-22 11:52发布

问题:

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

回答1:

Expanding on the other answers, declaring something with type asterisk is exactly the same as leaving it untyped.

var x:* = {};
var y = {}; // equivalent

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:

var o:Object = {};
o.foo = 1; // fine
var a:* = o;
a.bar = 1; // again, fine

var s:String = "";
s.foo = 1; // compile-time error
var b:* = s;
b.bar = 1; // run-time error

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:

http://livedocs.adobe.com/labs/air/1/aslr/specialTypes.html#*

(The markup engine refuses to linkify that, because of the asterisk.)



回答2:

It's a way of specifying an untyped variable so that you can basically assign any type to it. The code

var x:* = oneTypeObject;

creates the variable x then assigns the oneTypeObject variable to it. You can assign an entirely different type to it as well as follows:

var x:* = anotherTypeObject;

However, you still can't arbitrarily set or access properties; they have to exist in the underlying type (of either oneTypeObject or anotherTypeObject).

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.



回答3:

It's the "untyped" type. It just means that the variable can be of any type. Basically the same effect as using this:

var x = myObject;


回答4:

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:

var x:*;


回答5:

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:

var untyped:* = functionThatReturnsSomeValue();

But if you go one step farther, you have to watch out or you might get bitten:

var name:String = untyped.name;

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:

(elsewhere)
public class TypedObject()
{
    public var name:String = "";
}

(and the code at hand)
var typed:TypedObject = functionThatReturnsTypedObject();
var name:String = typed.name;