CKTIS / JavaScript
Object model and composition
JavaScript objects combine own properties, prototype delegation, and property descriptors. Learn how inheriting behavior differs from composition and how to control property reads, writes, and enumeration.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
Object.create(proto)
Object.defineProperty(obj, key, { value, writable, enumerable, configurable })
Object.defineProperty(obj, key, { get, set, enumerable, configurable })An object holds properties keyed by strings or Symbols. For ordinary objects, a read checks own properties first, then follows the prototype chain until null. An own property shadows an inherited one. Object.prototype is usually near the end of the chain, but Object.create(null) creates an object without a prototype.
Inheritance delegates lookup to another object; it does not copy its properties. Composition builds an object from parts, for example by keeping state in a nested object. In the example, player delegates the describe method to behavior, while stats is a component of the composed object. Calling player.describe() makes this refer to player.
Object.defineProperty defines or updates an own property. A data descriptor contains value and writable: the stored value and permission to change it through assignment. An accessor descriptor contains get and set: functions invoked on reads and writes. Mixing these two groups of fields throws TypeError. A missing accessor is undefined.
enumerable controls participation in operations such as Object.keys, which returns only own enumerable string keys. configurable: false prevents deletion and most descriptor changes, but still permits changing the value of a writable data property. When creating a property with Object.defineProperty, omitted enumerable and configurable fields default to false for both kinds of property. For a data property, omitted value defaults to undefined and omitted writable defaults to false. An accessor property's descriptor has neither value nor writable. Consequently, the example's id is absent from the keys and is not writable. Failed assignment to such a property on an ordinary object throws TypeError in strict mode and is ignored otherwise.
When an update does not change the property's kind, omitted fields retain their values. A property with configurable: true can switch from data to accessor or vice versa. When it does, the old kind-specific fields are discarded, and omitted fields specific to the new kind take their defaults: value, get, and set default to undefined, and writable defaults to false, as applicable. The shared fields enumerable and configurable keep their existing values if omitted.
Examples
"use strict";
const behavior = {
describe() { return `${this.name}:${this.score}`; }
};
const player = Object.create(behavior);
player.name = "Ada";
player.stats = { points: 2 };
Object.defineProperty(player, "score", {
get() { return this.stats.points; },
set(value) { this.stats.points = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(player, "id", { value: 7 });
player.score = 5;
console.log(player.describe());
console.log(Object.keys(player).join(","));
console.log(Object.getPrototypeOf(player) === behavior);
console.log(Object.getOwnPropertyDescriptor(player, "id").writable);Result
Ada:5 name,stats,score true false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.
Sources
- ECMAScript® Language Specification — Editor’s Draft
- ECMAScript® 2027 Language Specification — ECMAScript Data Types and Values
- ECMAScript® 2027 Language Specification — Fundamental Objects
- Official reference — developer.mozilla.org: Guide / Inheritance_and_the_prototype_chain
- Official reference — developer.mozilla.org: Object / defineProperty