3

I can see some methods defined on Object and some defined on Object.prototype. For some, I understand the reason but for few I didn't get rationale behind. Why is getOwnPropertyDescriptor defined on Object and not on Object.prototype?

halfer
  • 19,824
  • 17
  • 99
  • 186
Gautam
  • 815
  • 6
  • 15
  • 3
    The direct answer is "because `getOwnPropertyDescriptor` doesn't work on instances, you have to pass an object to it". If you want to know why it was decided to go that route, then perhaps we can point at stuff like `Object.keys()` that also accepts an object but isn't available on instances. But then if you want to know why the difference...I guess a language designer would have to chime in with the exact reason why the split between instance and static methods. – VLAZ Apr 04 '19 at 12:29
  • 1
    Related: [Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?](https://stackoverflow.com/questions/12017693) – adiga Apr 04 '19 at 12:39

1 Answers1

1

The Object.getOwnPropertyDescriptor method allows to query the full information about a property and returns a property descriptor for an own property (that is, one directly present on an object and not in the object's prototype chain) of a given object.

Where as Object prototype objects inherit properties and methods from a prototype.

and works in similar way but it is way more generic to add/update properties to Object

Sohan
  • 6,252
  • 5
  • 35
  • 56