<分区>
我正在阅读 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
不返回对象从其原型(prototype)继承中获得的属性和方法。所以我尝试了以下方法:
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)
返回的数组中,但是当我记录它的键时返回一个空数组?我是不是理解错了这段话?