我知道PHP中可能有"变量"变量.例如
$x = "variable";
$$x = "hello, world!";
echo $variable; // displays "hello, world!"
Run Code Online (Sandbox Code Playgroud)
是否可以在javascript中将变量的名称引用为字符串?怎么做?
假设我需要根据计数器声明一个javascript变量我该怎么办?
var pageNumber = 1;
var "text"+pageNumber;
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用?
能否请你帮忙?谢谢!
要为变量赋值是非常简单的,只需这样做即可
var foo = "bar";
Run Code Online (Sandbox Code Playgroud)
但是要为变量指定一个名称(动态),你必须这样做?
var variableName = "newName";
var variableName = "bar"; // in this case assign new value of variableName
Run Code Online (Sandbox Code Playgroud)
我必须这样做吗?
var foo + "_" + variableName = "foo" // foo_newName = "bar"
Run Code Online (Sandbox Code Playgroud)