I'm reading Eloquent JavaScript's Map section and I'm having trouble understanding its last paragraph:
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.
I then assume that Object.keys
does not return the properties and methods an object gets from the inheritance of its prototype. So I tried the following:
var anObject = {};
console.log(Object.keys(anObject)); //Array []
console.log("toString" in Object.keys(anObject)); //true
console.log(anObject.hasOwnProperty("toString")); //false
Apparently, toString
is in the array returned by Object.keys(anObject)
, but an empty array is returned when I logged its keys? Did I understand the passage wrongly?