Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the type of a var/name in Actionscript3?

Erm, that's it!...

like image 311
user84643 Avatar asked Aug 28 '09 03:08

user84643


People also ask

What is string data type in action script?

String data type Strings are immutable values, just as they are in the Java programming language. An operation on a String value returns a new instance of the string. The default value for a variable declared with the String data type is null . The value null is not the same as the empty string ( "" ).

How do I declare a variable in Actionscript?

To declare a new variable, we use the var statement. For example: var speed; var bookTitle; var x; The word var tells the interpreter that we're declaring a variable, and the text that follows, such as speed, bookTitle, or x, becomes our new variable's name.


5 Answers

flash.utils.getQualifiedClassName(...)

You can pass any ActionScript value to this function to get a String containing its fully qualified class name.

like image 195
Jacob Wan Avatar answered Oct 21 '22 14:10

Jacob Wan


The function is called typeof(). http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#typeof

like image 34
Chiwai Chan Avatar answered Oct 21 '22 14:10

Chiwai Chan


If you only need the most fundamental description of it's type, then you can use the typeof operator, like so:

var foo:String = "test";
trace( typeof foo );
// string

While this is convenient it has a drawback. That being it always gives the base type of the variable, for example:

var foo:Array = ["A","B","C","D"];
trace( typeof foo );
//object

var bar:int = 5;
trace( typeof bar );
//number

var hummer:Car = new Car();
trace( typeof hummer );
//Vehicle

Which are both technically right, but may not be what you're looking for.

If you want the more specific type (ie Array, String etc.) then you need to use the slightly more complicated getQualifiedClassName() function from the flash.utils package:

import flash.utils.getQualifiedClassName;

var foo:Array = ["A","B","C","D"];
trace( getQualifiedClassName( foo ) );
//Array

var bar:int = 5;
trace( getQualifiedClassName( bar ) );
//int

var hummer:Car = new Car();
trace( getQualifiedClassName( hummer ) );
//Car

typeof documentation

getQualifiedClassName() documentation

like image 22
Kris Welsh Avatar answered Oct 21 '22 13:10

Kris Welsh


If memory serves me right, a method flash.utils.describeType hands you an xml document with all reflected typeinfo of an object/type.

Indeed: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#describeType%28%29

like image 24
spender Avatar answered Oct 21 '22 13:10

spender


The is operator is the up to date solution:

var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite);           // true

See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#is

like image 36
Ross Attrill Avatar answered Oct 21 '22 12:10

Ross Attrill