繁体   English   中英

关联数组:定义未定义?

[英]associative array: define the undefined?

我刚刚尝试了以下代码(在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]);

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

提前谢谢

那是因为

'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是另一个值。 当您在字典查找中使用这两个函数时,它们将返回(并且应该返回)不同的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM