inspection
Object.getPrototypeOf
Returns the prototype (i.e. the internal [[Prototype]]) of the specified object, or null if it has none. Preferred over the legacy __proto__ accessor.
Object.getPrototypeOf(obj) Parameters
| Parameter | Purpose |
|---|---|
| obj | The object whose prototype is to be returned |
| returns | The prototype object or null |
Examples
console.log(Object.getPrototypeOf({}) === Object.prototype); Logs true
console.log(Object.getPrototypeOf([]) === Array.prototype); Logs true
const bare = Object.create(null);
console.log(Object.getPrototypeOf(bare)); Logs null — bare object has no prototype
class Animal {}
class Dog extends Animal {}
console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype); Logs true — confirms the class inheritance chain
Gotcha
For primitives it returns the prototype of the boxed wrapper (e.g. String.prototype for a string). Prefer this over the legacy `__proto__` property, which is deprecated for reads and writes.
Related methods
Object.setPrototypeOf
Sets the prototype (i.e. the [[Prototype]]) of a specified object to another object or null. Considered a slow operation and generally discouraged — prefer Object.create at construction time.
Object.create
Creates a new object with the specified prototype and optional property descriptors. Pass null to create an object with no prototype at all.
Object.isExtensible
Returns true if new properties can be added to the object. Sealed, frozen, and preventExtensions'd objects all return false; primitives always return false.
Object.keys
Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.
Object.values
Returns an array of a given object's own enumerable string-keyed property values. Introduced in ES2017 as a companion to Object.keys.