开发者

Variable of type asterisk

开发者 https://www.devze.com 2023-03-06 16:02 出处:网络
var test:*; test = sMC // Some movieClip exported for ActionScript var f = new test; Sorry if the question\'s a bit lame, but I begin to wonder, wh开发者_如何学编程at does this asterisk, and the sni
var test:*;
test = sMC // Some movieClip exported for ActionScript
var f = new test;

Sorry if the question's a bit lame, but I begin to wonder, wh开发者_如何学编程at does this asterisk, and the snippet mean?


Answering your original question and your question asked in a comment:

An asterisk is a wildcard which means the variable will accept any type of info. Example:

var wildcard:*;

wildcard = "hello";
wildcard = 10;
wildcard = new MovieClip();

All of the above will work.

Variables should be typed as strictly as possible; by this I mean that when you want to assign a MovieClip to a variable, your variable should be typed as a MovieClip. Like so:

var mc:MovieClip = new MovieClip();

This works for anything. If you create your own class, then use that as your type for a variable that holds your class.

var thing:MyClass = new MyClass();

An error will be thrown if you try and assign an unrelated type to a variable, like so:

var thing:MovieClip = "hello";

But as long as your variable type is somewhere along the inheritance chain of what you're assigning to it, then it will work.

var thing:DisplayObject = new MovieClip();

This can be handy if you want to loop through an array containing an assortment of your own classes that extend MovieClip.

var ar:Array = [];

/**
 * MyClass extends MovieClip
 * MyOtherClass extends MovieClip
 */

ar.push(new MyClass());
ar.push(new MovieClip());
ar.push(new MyOtherClass());

var i:MovieClip;
for each(i in ar)
{
    trace(i);
}

Overall the wildcard type is not a recommendation. At worst use Object as everything in flash extends this. One situation where a wildcard or Object can be useful is if you want to create a function that can accept any kind of data. Like so:

var myarray:Array = [];

function addToArray(data:Object):void
{
    myarray[myarray.length] = data;
    trace(data);
}

OR

function addToArray(data:*):void
{
    myarray[myarray.length] = data;
    trace(data);
}

Hope this all makes sense.


The asterisk means the variable type is undefined, or a wildcard. Meaning you can define test as any sort of variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消