title: Javascript new object instance from dynamic function name
body: <p>In PHP a string can be used to dynamically select a class for instantiation.  Here are 2 simple classes in PHP:</p>
  
  <pre><code>&lt;?php
  class Magic implements Genius {
    public function perform() {
      echo 'madya look :P' . PHP_EOL;
    }
  }
  
  class Genie implements Genius {
    public function perform() {
      echo 'your wish has been granted!' . PHP_EOL;
    }
  }
  </code></pre>
  
  <p>Now there could be a variable such that the class instantiated at runtime is based upon its contents</p>
  
  <pre><code>$sGeniusClass = 'Magic';
  $oGenius      = new $sGeniusClass();
  </code></pre>
  
  <p>Now in Javascript I like to use functions as constructors to have some level of typed-ness, in that vein I might have:</p>
  
  <pre><code>function Magic() {}
  Magic.prototype = {
    perform : function()
    {
      console.log('madya look :P');
    }
  }
  
  function Genie() {}
  Genie.prototype = {
    perform : function()
    {
      console.log('your wish has been granted!');
    }
  }
  </code></pre>
  
  <p>I know I can pull off something similar to PHP with an <em>eval</em>:</p>
  
  <p><strong>Approach #1</strong></p>
  
  <pre><code>var sClassName = 'Genie';
  eval('var oGenius = new ' + sClassName);
  </code></pre>
  
  <p>I've also seen <a href="http://bytes.com/topic/javascript/answers/682387-class-forname" rel="noreferrer">an approach</a> that makes a call to the <strong>Function</strong> function:</p>
  
  <p><strong>Approach #2</strong></p>
  
  <pre><code>var sClassName = 'Genie';
  var oGenius = new Function('return new ' + sClassName)();
  </code></pre>
  
  <p><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function" rel="noreferrer">On the MDN</a> though it sounds like this suffers performance from re-evaluation every time an instance is created:</p>
  
  <blockquote>
    <p>Function objects created with the Function constructor are parsed when
    the function is created. This is less efficient than declaring a
    function and calling it within your code, because functions declared
    with the function statement are parsed with the rest of the code.</p>
  </blockquote>
  
  <p>Now I have one more approach that is a bit tedious:</p>
  
  <p><strong>Approach #3</strong></p>
  
  <pre><code>var aClassMap = {
    Magic : Magic,
    Genie : Genie,
    create : function(sClassName) {
      if(this[sClassName] === undefined)
        return false;
      return new this[sClassName];
    }
  }
  
  var sClassName = 'Genie';
  var oGenius = aClassMap.create(sClassName);
  </code></pre>
  
  <p>It seems to be my favorite overall, no use of <em>eval</em>, and no subsequent re-evaluation, like solution #2.  It's still a bit of work though, so my question, twofold:</p>
  
  <ol>
  <li>Are there any other ways to do this other than the 3 approaches I've shown?</li>
  <li>Is there something similar to PHP where a string can map to a function to be instantiated?</li>
  </ol>