尝试获取变量的值并使其成为另一个变量的名称.JavaScript的

Jac*_*nML 0 javascript string variables var

我正在尝试获取函数的输入(objectName)并使该值创建变量.因此,如果objectName ="abcd",我希望创建变量为abcd.有没有办法在JavaScript中做到这一点?

小智 5

你可以window[variableName]像这样使用:

//Variable name
var name = "abc";

//window[name] is equal to the variable abc
window[name] = 123;

//show 123
alert(abc);
Run Code Online (Sandbox Code Playgroud)

小提琴