关联数组:定义undefined?

时间:2013-09-14 21:25:32

标签: javascript regex arrays associative-array

我刚刚尝试了以下代码(在javascript中),该代码应该返回“这是完全未定义的”,这不起作用:

 var foo = {'foo' : 'bar', undefined : 'This is totally undefined!'};
 alert( foo['toomany foobars'.match(/asdf/)] );

然而这完美无缺:

 var foo = {'foo' : 'bar', undefined : 'This is totally undefined!'};
 alert(foo[undefined]);

我不明白,是否有人可以解释?

提前多多感谢

1 个答案:

答案 0 :(得分:1)

那是因为

'toomany foobars'.match(/asdf/) === null
null !== undefined

现在,另一方面,如果你有这个

var foo = {'foo' : 'bar', undefined : 'This is totally undefined!', null: 'and this one is null'};
alert( foo['toomany foobars'.match(/asdf/)] );

你会看到它有效。


发生这种情况的原因是undefined是JavaScript中的值,null是另一个值。当您在字典查找中使用这两个时,它们将返回(并应返回)不同的结果。

相关问题