0

As far as my understanding about it, every object I create is also an Object of its prototype. now the in keyword should tell me about existence of my object and its prototypes.

So in this case, why shouldn't I get a true result?

function test() {
  var myObj = {
    "hello": "hell"
  };
  alert("keys" in myObj); // false -- why ?
  alert("keys" in Object) // true
}
Roni Axelrad
  • 399
  • 3
  • 13
  • 7
    `keys` is a function on `Object`, not `Object.prototype`. You always use it as `Object.keys(myObj)`. – Ry- Jan 02 '16 at 06:00
  • But even if it's a function, the 'in' keyword recognizes it as a property of Object, which is great, but myObj is also an Object, and keys is not found on it . – Roni Axelrad Jan 02 '16 at 06:03
  • 2
    `Object.keys` is static method and it does not get inherited from parent – Bek Jan 02 '16 at 06:12
  • Oh !! OK got it . that make sense ! – Roni Axelrad Jan 02 '16 at 06:14
  • @RoniAxelrad: The inherited things are on `Object.prototype`. Compare: `alert('toString' in myObj); alert('toString' in Object.prototype);`. Using `Object` as an example is slightly confusing, because `Object` is an instance of itself, so `'toString'` is also on `Object`. – Ry- Jan 02 '16 at 06:33
  • @RyanO'Hara Yeah I didn't know that before. Thanks a lot for your help .I Really thought that all Objects's properties are part of the prototype. – Roni Axelrad Jan 02 '16 at 06:35
  • https://es5.github.io/#x11.8.7 From the spec the `in` operator essentially resolves a string and an object using [[OwnProperty]] or (hasOwnProperty). – t3dodson Jan 02 '16 at 06:36
  • See also http://stackoverflow.com/questions/19941499/why-is-object-keys-not-object-prototype-keys. –  Jan 02 '16 at 07:45
  • That different, that was actually my answer. – Roni Axelrad Jan 02 '16 at 15:14

0 Answers0