Home > Enterprise >  Why Object.getOwnPropertyDescriptors() does not work when I pass the history object (which is part o
Why Object.getOwnPropertyDescriptors() does not work when I pass the history object (which is part o

Time:01-24

There are different objects in the browser object model (BOM) such as history, location, document, etc. I can get the property descriptors for the location and the document objects but not for the history object. Why is that so?

enter image description here

CodePudding user response:

Because the history object instance has no "own" properties. All of them are inherited from its prototype, which by definition are not included in Object.getOwn[anything] functions. You will also notice that Object.getOwnPropertyNames(history) is an empty array when you run it, telling us as much.

(Unlike location or document, which are objects with defineOwnProperty steps as part of their formal instantiation specification, history is an "empty" object with fairly special handling)

Instead, use Object.getOwnPropertyDescriptors(Object.getPrototypeOf(history)) if you want to see its "immediate" API.

  •  Tags:  
  • Related