javascript中的变量类名 [英] Variable classname in javascript

查看:146
本文介绍了javascript中的变量类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以在javascript中创建变量类名。在php中,下一个是允许的:

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

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

Tom

推荐答案

使用方括号

some_object["string_containing_method_name"]();

如果你想玩全局变量,那么只要记住它们都是 window object。

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

...但不要使用全局

您的特定示例:

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

(显然,在机器生成的代码中只使用$的常规约定也应该适用)

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

这篇关于javascript中的变量类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆