我知道PHP中可能有"变量"变量.例如
$x = "variable";
$$x = "hello, world!";
echo $variable; // displays "hello, world!"
Run Code Online (Sandbox Code Playgroud)
是否可以在javascript中将变量的名称引用为字符串?怎么做?
<script>
//in one script
var someVarName_10 = 20;
</script>
Run Code Online (Sandbox Code Playgroud)
我希望通过变量名称从另一个脚本访问此变量.窗口对象很简单,是否可以使用局部变量?
我的意思是通过以下代码访问此var:
<script>
alert(all_vars['someVar' + 'Name' + num]);
</script>
Run Code Online (Sandbox Code Playgroud) 可能重复:
有没有办法使用包含变量名称的字符串访问javascript变量?
JavaScript:通过名称字符串动态获取局部变量
一个简单的代码来说明我的问题 -
var chat_1 = "one";
var chat_2 = "two";
var id = "1";
var new = ?? variabalize( 'chat_' + id )
Run Code Online (Sandbox Code Playgroud)
我希望变量new赋值变量 - chat_1是"one"
如何使后面的代码工作?
var x = 'name';
Run Code Online (Sandbox Code Playgroud)
然后,使用x中的值,因为它是一个变量,并设置它,所以如果我想让它成为NAME,我将得到结果:
var name = 'NAME';
Run Code Online (Sandbox Code Playgroud)
可能吗 ?
我有50个名为animation0,animation1,animation2等的svg动画,我想在将0到49的整数传递给此函数时加载它们:
function loadAnimation(value){
var whichswiffy = "animation" + value;
var stage = new swiffy.Stage(document.getElementById('swiffycontainer'), whichswiffy);
stage.start();
}
Run Code Online (Sandbox Code Playgroud)
它现在不起作用,也许它正在传递'哪些'而不是动画10?
有任何想法吗?