Does Object.keys(anObject) return anObject's prototype?
You understood the passage correctly, the usage of in
here is wrong though.
It returns true, since the Array that Object.keys(anObject)
returns has a property called toString
in it's prototype. in
also checks the prototype, while hasOwnProperty
only checks the properties an Object actually has.
But it doesn't have an own property with the name toString
let anObject = {};
let keys = Object.keys(anObject);
console.log("toString" in keys); //true
console.log(keys.hasOwnProperty("toString")); //false
Object.keys(anObject)
returns an array, and toString
is a valid method on arrays.
Since in
checks for a property in the object, and in its prototype chain, the expression "toString" in Object.keys(anObject)
returns true
.
But this doesn't mean that toString
is an owned property of anObject
. That's why anObject.hasOwnProperty("toString")
returns false
.