参见英文答案 >
Why does javascript’s “in” operator return true when testing if 0 exists in an array that doesn’t contain 0?4个
我正在阅读 Eloquent JavaScript’s Map section,我无法理解它的最后一段:
我正在阅读 Eloquent JavaScript’s Map section,我无法理解它的最后一段:
If you do have a plain object that you need to treat as a map for some reason,it is useful to know that
Object.keys
returns only an object’s own keys,not those in the prototype. As an alternative to thein
operator,you can use thehasOwnProperty
method,which ignores the object’s prototype.
然后我假设Object.keys不返回对象从其原型的继承中获取的属性和方法.所以我尝试了以下内容:
var anObject = {}; console.log(Object.keys(anObject)); //Array [] console.log("toString" in Object.keys(anObject)); //true console.log(anObject.hasOwnProperty("toString")); //false
显然,toString位于Object.keys(anObject)返回的数组中,但是当我记录其键时会返回一个空数组?我错误地理解了这段经文吗?