Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The accepted (and currently most popular answer) has some flaws. The answer serves for this specific use case, but the comments have expanded the answer to a seeming general solution. </p> <p>But it is not a type-safe solution in certain cases, and it doesn't address all possible objects. The idea that XML is not supported has been addressed well enough here and elsewhere, but the type-safe idea has not.</p> <p>The assumption made is that it is an class object created by the programmer. Here are some tests that I set up (this is in strict mode, but a local test). Note the <code>int</code> test results:</p> <pre><code>var sprite:Sprite = new Sprite(); var xml:XML = new XML(); var testInt:int = 2; var testClass:TestClass = new TestClass(); var testAnon:Object = {}; trace("classname 1 = " + getQualifiedClassName(sprite)); trace("myclass 1 = " + getDefinitionByName(getQualifiedClassName(sprite))); trace("constructor a 1 = " + Object(sprite).constructor); trace("constructor a 1 = " + (Object(sprite).constructor as Class)); trace("constructor b 1 = " + sprite["constructor"]); trace("constructor b 1 = " + (sprite["constructor"] as Class)); trace("..."); trace("classname 2 = " + getQualifiedClassName(xml)); trace("myclass 2 = " + getDefinitionByName(getQualifiedClassName(xml))); trace("constructor a 2 = " + Object(xml).constructor); trace("constructor a 2 = " + (Object(xml).constructor as Class)); trace("constructor b 2 = " + xml["constructor"]); trace("constructor b 2 = " + (xml["constructor"] as Class)); trace("..."); trace("classname 3 = " + getQualifiedClassName(testInt)); trace("myclass 3 = " + getDefinitionByName(getQualifiedClassName(testInt))); trace("constructor a 3 = " + Object(testInt).constructor); trace("constructor a 3 = " + (Object(testInt).constructor as Class)); trace("constructor b 3 = " + testInt["constructor"]); trace("constructor b 3 = " + (testInt["constructor"] as Class)); trace("..."); trace("classname 4 = " + getQualifiedClassName(testClass)); trace("myclass 4 = " + getDefinitionByName(getQualifiedClassName(testClass))); trace("constructor a 4 = " + Object(testClass).constructor); trace("constructor a 4 = " + (Object(testClass).constructor as Class)); trace("constructor b 4 = " + testClass["constructor"]); trace("constructor b 4 = " + (testClass["constructor"] as Class)); trace("..."); trace("classname 5 = " + getQualifiedClassName(testAnon)); trace("myclass 5 = " + getDefinitionByName(getQualifiedClassName(testAnon))); trace("constructor a 5 = " + Object(testAnon).constructor); trace("constructor a 5 = " + (Object(testAnon).constructor as Class)); trace("constructor b 5 = " + testAnon["constructor"]); trace("constructor b 5 = " + (testAnon["constructor"] as Class)); trace("..."); </code></pre> <p>With <code>TestClass</code> defined as:</p> <pre><code>package { public class TestClass { } } </code></pre> <p>Results:</p> <pre><code>classname 1 = flash.display::Sprite myclass 1 = [class Sprite] constructor a 1 = [class Sprite] constructor a 1 = [class Sprite] constructor b 1 = [class Sprite] constructor b 1 = [class Sprite] ... classname 2 = XML myclass 2 = [class XML] constructor a 2 = constructor a 2 = null constructor b 2 = constructor b 2 = null ... classname 3 = int myclass 3 = [class int] constructor a 3 = [class Number] constructor a 3 = [class Number] constructor b 3 = [class Number] constructor b 3 = [class Number] ... classname 4 = src::TestClass myclass 4 = [class TestClass] constructor a 4 = [class TestClass] constructor a 4 = [class TestClass] constructor b 4 = [class TestClass] constructor b 4 = [class TestClass] ... classname 5 = Object myclass 5 = [class Object] constructor a 5 = [class Object] constructor a 5 = [class Object] constructor b 5 = [class Object] constructor b 5 = [class Object] ... </code></pre> <p>Beyond any current testing, there is fairly good reason to use <code>getDefinitionByName</code> over the <code>constructor</code> methods. The <code>getDefinitionByName</code> method allows :</p> <ul> <li>Adobe to develop internal solutions for the (current and future) problematic areas</li> <li>you would not have to change your code for future Adobe developments</li> <li>you do not have to develop two (or more) separate methods of dynamic instantiation.</li> </ul> <p>It may be slower now, but in the future there may be improvements from Adobe's side that would address the speed issue.</p> <p>(For example, it used to be that <code>uint</code> was far slower in for-loops than <code>int</code>, so a lot of conversion code was set up to use the faster <code>int</code>. It was a fairly simple issue to solve, so Adobe fixed it, and now there is a lot of code that jumps through some unnecessary hoops to achieve an outdated goal.)</p> <p>Since the <code>getDefinitionByName</code> method works correctly in all cases, code should be developed using that method, and then work to get Adobe to fix the speed issue. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload