Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get from an instance of a class to a Class object in ActionScript 3?

How do you get an instance of the actionscript class Class from an instance of that class?

In Python, this would be x.__class__; in Java, x.getClass();.

I'm aware that certain terrible hacks exist to do this, but I'm looking for a built-in language facility, or at least a library routine built on something reliable.

like image 381
Glyph Avatar asked Oct 15 '08 07:10

Glyph


People also ask

What is a method in ActionScript?

A function is called a method if you define it as part of a class definition or attach it to an instance of an object. A function is called a function closure if it is defined in any other way. Functions have always been extremely important in ActionScript.

What is a class in ActionScript?

Classes are external ActionScript files that define objects; they are stored and organized in a specific directory structure in the Flash CS3 program directory (Figure 4.1). To use them in your code, you have to import them.

What is the keyword used to create a variable in ActionScript?

Creating a variable is called variable declaration. ActionScript 3.0 requires the use of a keyword— var —to signal that you are declaring a variable.


2 Answers

You can get it through the 'constructor' property of the base Object class. i.e.:

var myClass:Class = Object(myObj).constructor; 
like image 113
Gerald Avatar answered Sep 22 '22 08:09

Gerald


Any reason you couldn't do this?

var s:Sprite = new flash.display.Sprite();  var className:String = flash.utils.getQualifiedClassName( s ); var myClass:Class = flash.utils.getDefinitionByName( className ) as Class;  trace(className ); // flash.display::Sprite trace(myClass); // [class Sprite]  var s2 = new myClass(); trace(s2); // [object Sprite] 

I don't know a way to avoid round-tripping through a String, but it should work well enough.

like image 24
fenomas Avatar answered Sep 18 '22 08:09

fenomas