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

后端 未结 3 1725
感情败类
感情败类 2021-01-30 08:39

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,

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 09:38

    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.

提交回复
热议问题