Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable classname in javascript

Is there a way to make a variable classname in javascript. In php the next is allowed:

$classname = "klasse";
$class = new $classname();

Tom

like image 905
Timo Avatar asked Sep 12 '25 19:09

Timo


1 Answers

Use square bracket notation:

some_object["string_containing_method_name"]();

If you want to play with globals, then just remember they are all properties of the window object.

… but don't play with globals.

Your particular example:

var $classname = "klasse";
var $class = new window[$classname]();

(Obviously, the usual conventions for only using $ in machine generated code should apply too)

like image 196
Quentin Avatar answered Sep 14 '25 07:09

Quentin