检查不确定性不是测试密钥是否存在的准确方法。如果密钥存在但值实际上是
undefined怎么办?
var obj = { key: undefined };obj["key"] !== undefined // false, but the key exists!您应该改为使用
in运算符:
"key" in obj // true, regardless of the actual value
如果要检查密钥是否不存在,请记住使用括号:
!("key" in obj) // true if "key" doesn't exist in object!"key" in obj // ERROR! Equivalent to "false in obj"或者,如果您要特别测试对象实例的属性(而不是继承的属性),请使用
hasOwnProperty:
obj.hasOwnProperty("key") // true有关和的方法之间的性能比较
in,
hasOwnProperty关键是
undefined



