我有两个变量:的Javascript,添加字符串数组[字符串]
var cost = new Array();
var maxslot = new Array();
和我这样设置值:
<script>
cost = {"m2mp":"0.05"};
maxslot = {"m2mp":"1000"};
</script>
<script>
cost = {"samp":"0.04"};
maxslot = {"samp":"500"};
</script>
但samp
取代m2mp
,如果我叫alert(cost["m2mp"])
,它输出undefined
(如果我不分配samp
,我会得到正确的输出)。我尝试用+=
替换=
,但它没有帮助(然后samp
和m2mp
都是undefined
)。
我找不到任何解决方案 “模拟” 这样的:
cost["string"] = string;
为什么你甚至有单独的脚本?在一个脚本中,你可以简单地写'cost = {m2mp:0.05,samp:0.04); maxslot = {m2mp:0.04,samp:500);'我只是假设值应该被视为数字。 – MCL