Javascript 关联数组:定义未定义的数组?
我刚刚尝试了以下代码(javascript),该代码应返回“This is verally undefined”,但不起作用:Javascript 关联数组:定义未定义的数组?,javascript,regex,arrays,associative-array,Javascript,Regex,Arrays,Associative Array,我刚刚尝试了以下代码(javascript),该代码应返回“This is verally undefined”,但不起作用: var foo = {'foo' : 'bar', undefined : 'This is totally undefined!'}; alert( foo['toomany foobars'.match(/asdf/)] ); 然而,这是完美的: var foo = {'foo' : 'bar', undefined : 'This is totally u
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
是另一个值。当您在字典中使用这两个查找时,它们将返回(并且应该返回)不同的结果。我刚刚尝试定义null,它返回“这是完全未定义的”,谢谢。。。很抱歉我自己没有想到这一点