Search code examples
javascriptobjectecmascript-6prototype

Does Object.keys(anObject) return anObject's prototype?


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 the in operator, you can use the hasOwnProperty 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?


Solution

  • 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 (meaning the properties that were "manually added" to the empty Object).

    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, since there is a property "toString" in the keys.prototype object
    console.log(keys.hasOwnProperty("toString")); //false, since there aren't any properties in the object itself