[英]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.