Skip to content

Descriptors

Eugene Lazutkin edited this page Aug 15, 2024 · 9 revisions

JavaScript provides a number of functions that work with descriptors:

This module contains a collection of helpers to simplify some common tasks with descriptors.

The sister module aliases.js provides a set of helpers to alias existing properties using utilities from this module.

descriptors.js

Legend for tables
  • API
    • descriptora property descriptor.
    • getter — a function with no arguments that returns a value.
    • setter — a function with one argument that sets a value.
    • defaultDescriptor — a descriptor that is used to set default values for getters/setters. Defaults to {enumerable: true, configurable: true}.
    • target — an object that will receive the descriptor.
    • source — an object that contains the descriptor.
    • force — if truthy, the descriptor will be added even if it already exists in the target.
    • names — an array of names as strings or symbols, a symbol, or a string containing a comma-separated list of names.

The following utilities are available:

Function Return value Description
makeGetter(getter, defaultDescriptor) descriptor Creates a getter descriptor.
makeSetter(setter, defaultDescriptor) descriptor Creates a setter descriptor.
makeAccessors(getter, setter, defaultDescriptor) descriptor Creates a getter and setter descriptor.
addDescriptor(target, names, descriptor, force) target Adds a descriptor to an object.
addDescriptors(target, dict, force) target Adds multiple descriptors to an object.
addGetters(target, dict, force) target Adds multiple getters to an object.
copyDescriptors(target, source, names, force) target Copies multiple descriptors from one object to another.

addDescriptors() uses descriptor objects organized as a dictionary. Its keys can be symbols or strings. Strings are treated as comma-separated lists of names.

addGetters() are similar to addDescriptors(), but instead of adding descriptors, descriptors are generated from functions. Internally, descriptors are created from those functions using makeGetter() with default values.

copyDescriptors() copies descriptors from one object to another (it can be the same object). names can be one of the following:

  • A string containing a comma-separated list of names.
  • A symbol.
  • An array of symbols or strings.
  • An object with keys as symbols or strings denoting a name of a descriptor from the source object. An associated value is a value suitable as the names argument of addDescriptors().

Examples

import {addGetters, copyDescriptors} from 'meta-toolkit/descriptors.js';

class Foo {
  constructor() { this.value = 0; }
  get triple() { return this.value * 3; }
}

addGetters(Foo.prototype, {
  half() { return this.value / 2; },
  double() { return this.value * 2; }
});

class Bar {
  constructor() { this.value = 0; }
}

copyDescriptors(Bar.prototype, Foo.prototype, 'triple, double');

class Baz {
  constructor() { this.value = 0; }
}

copyDescriptors(Baz.prototype, Foo.prototype, {
  double: 'x2',
  triple: 'x3'
});

Exports

All functions are exported by their names. There is no default export.

Clone this wiki locally