Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Object.prototype, property descriptors, and accessors

Separate where a property is found in the prototype chain from how it behaves: it can store a value or use a getter or setter.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

Object.prototype
Object.getPrototypeOf(obj)
Object.hasOwn(obj, key)
Object.getOwnPropertyDescriptor(obj, key)
Object.defineProperty(obj, key, { value: 1, writable: true, enumerable: true, configurable: true })
Object.defineProperty(obj, key, { get() { return this._value; }, set(value) { this._value = value; }, configurable: true })

Object.prototype is the shared object at the end of many prototype chains, including that of {}. Its own prototype is null. An object made with Object.create(null) does not inherit from it. Use Object.getPrototypeOf(obj) to inspect the immediate prototype rather than the legacy __proto__ accessor, which can be absent or shadowed. Avoid adding application properties to Object.prototype: other objects can inherit them.

A descriptor describes a property's attributes. A data property stores a value, and writable controls whether assignment can change it. An accessor property instead has get and set, each a function or undefined. Both kinds have enumerable (affecting, for example, whether an own string key appears in Object.keys) and configurable (controlling deletion and restricting attribute changes). An accessor is a kind of property, not an alternative to a descriptor.

For ordinary objects, a read checks own properties first, then the prototype chain. Reading a data property returns its stored value; reading through a getter calls it with the original object as this in obj.key. With no getter, the read returns undefined. Assignment to an inherited accessor calls its setter with the same this; assignment to an inherited writable data property creates an own property if the object is extensible. A missing setter or writable: false blocks assignment: it throws TypeError in strict mode and leaves the property unchanged otherwise.

Object.hasOwn(obj, key) checks only own properties; key in obj also includes inherited ones. Object.getOwnPropertyDescriptor(obj, key) returns a separate object describing an own property, or undefined if there is none. It does not call the inspected property's getter on an ordinary object. Changing fields of the returned descriptor does not change the property.

Object.defineProperty(obj, key, descriptor) defines or updates an own property without calling an inherited setter. For a new property, omitted flags writable (for data), enumerable, and configurable default to false; when updating without changing the kind, omitted attributes retain their values. Combining value or writable with get or set, or attempting a forbidden redefinition, throws TypeError even outside strict mode.

Examples

(() => {
  'use strict';
  const proto = {
    points: 2,
    get score() { return this.points * 2; },
    set score(value) { this.points = value / 2; }
  };
  const player = Object.create(proto);
  const accessor = Object.getOwnPropertyDescriptor(proto, 'score');
  console.log(`${'score' in player} ${Object.hasOwn(player, 'score')} ${typeof accessor.get}`);

  player.score = 12;
  console.log(`${player.score} ${player.points} ${proto.points}`);

  Object.defineProperty(player, 'score', { value: 99 });
  const data = Object.getOwnPropertyDescriptor(player, 'score');
  console.log(`${player.score} ${player.points} ${data.writable} ${data.enumerable} ${data.configurable}`);
})();

Result

true false function
12 6 2
99 6 false false false

The `score` setter writes an own `player.points` without changing `proto.points`. Then `Object.defineProperty` shadows the accessor with an own data property without calling the setter.

Related lessons

  • JavaScript · Lesson 17 · Advanced · Version: 4

Sources