按名称访问JavaScript变量的值?
问题描述:
您好可以按名称访问JavaScript变量的值吗?示例:
Hello it is possible to access the value of a JavaScript variable by name? Example:
var MyVariable = "Value of variable";
function readValue(name) {
....
}
alert(readValue("MyVariable"));
这是可能的,因此输出是变量的值吗?如果是,我该如何写这个函数?
Is this possible, so that the output is "Value of variable"? If yes, how do I write this function?
谢谢
答
全局变量在窗口
对象上定义,因此您可以使用:
Global variables are defined on the window
object, so you can use:
var MyVariable = "Value of variable";
alert(window["MyVariable"]);