在javascript中使用字符串后面的[]

在javascript中使用字符串后面的[],javascript,Javascript,以下警报为100。我希望它能提醒200,但显然我遗漏了什么 $blah[1] = 100; function updateBlah(e) { $blah[e] = 200; alert($blah[e]); } updateBlah(1); 你需要先申报$blah。例如: var $blah = []; // <-- Declare $blah as an array $blah[1] = 100; // <-- Set array index 1 to 100 f

以下警报为100。我希望它能提醒200,但显然我遗漏了什么

$blah[1] = 100;
function updateBlah(e) {
    $blah[e] = 200;
    alert($blah[e]);
}
updateBlah(1);

你需要先申报$blah。例如:

var $blah = []; // <-- Declare $blah as an array
$blah[1] = 100; // <-- Set array index 1 to 100
function updateBlah(e) {
    $blah[e] = 200;
    alert($blah[e]);
}
updateBlah(1);

var$blah=[];// 您的代码无法正常工作。您需要先初始化数组,然后才能开始操作它

$blah = []; // You need this!
$blah[1] = 100;
function updateBlah(e) {
    $blah[e] = 200;
    alert($blah[e]);
}
updateBlah(1);
警报
200
,如预期

以下是Javascript中有关数组的一些参考资料:


为什么对变量使用
$
?!JS不是PHP。是否缺少
$blah=[]
?会发生什么?有错误吗?记住,
$
只是一个字符。
$blah
Fblah
没有区别。如果要在变量名中使用
$
,请直接使用。请注意,很多JS开发人员都会在变量名前面加上
$
,以表示它是jQuery包装的对象。谢谢。我错过了报税表。