This is a common Javascript question.  Note that `[]` is the dynamic equivalent 
of `.`.  That is:

  new lz.class1()

can be written:

  new a['class1']()

which means you can say:

  var whichclass = '1';

  new lz['class' + whichclass]();

On 2010-04-02, at 07:01, [email protected] wrote:

> I have multiple classes which I need to dynamically instantiate at run-time, 
> based on object definition data stored in a database.  Rather than the arduos 
> approach of using a large and redundant switch-case statement to deal with 
> each and every class type, is there a way to store the classes in, say, an 
> array of classes? (my terminology might not be quite correct there)   Then 
> instantiate any given class type by indexing into the array with the 
> appropriate index (which would be stored in the database as part of the 
> object definition data).   
> 
> For example, instead of...
> switch ( whichclass ) 
> {
>     case '1':
>         new lz.class1();
>         break;
> 
>     case '2':
>         new lz.class2();
>         break;
> 
>     case '3':
>         new lz.class3();
>         break;
> }
> 
> Is there a way to do...
> new lz.classes[whichclass]();
> 
> 
> 
> 
> 


Reply via email to