Wednesday, June 29, 2011

How to create instants for dynamic class name in js?

In js we can create a instant for class just like new classname(). If the you going to call a dynamic class name mean we meet a problem

(ex) if you going to call two diff class name based on selected object

name = 'test'
new 'class'+name+'()';

JS will show u on error, we  can overcome with eval() function. just use

eval ('calss'+name+'()');

also you can pass arguments like this

eval ('calss'+name+'(arg1,arg2)');

No comments:

Post a Comment