Skip to content

Aliases

Eugene Lazutkin edited this page Feb 24, 2026 · 6 revisions

This module is based on descriptors.js and provides helpers to create aliases withing an object. It is done by copying descriptors.

aliases.js

This micro-module is formulated in terms of descriptors.js utilities. See src/aliases.js for details.

The following utilities are available:

Function Return value Description
addAlias(object, name, aliases, force) object Add an alias to an object.
addAliases(object, dict, force) object Add aliases to an object.

addAlias() takes the following arguments:

  • object — The object to add the alias to.
  • name — The name of the property to alias. It can be a string or a symbol.
  • aliases — The alias(es) to add. It is used as the names argument of addDescriptor() of descriptors.js.
    • If it is a string it is treated as a comma-separated list of names.
    • It can be a symbol.
    • It can be an array of symbols or strings.
  • force — If truthy, then the alias will be added even if it is already there.

addAliases() differs from addDescriptors() in that it adds multiple aliases to an object using the special dict argument. It is used as the names argument of copyDescriptors() of descriptors.js. In this case, only the last form (a dictionary) makes sense:

  • An object with keys as symbols or strings denoting a name of a descriptor from the object. An associated value is a value suitable as the names argument of addDescriptors().

Examples

import {addAlias, addAliases} from 'meta-toolkit/aliases.js';

class Foo {
  constructor() {
    this.value = 0;
  }
  get double() {
    return this.value * 2;
  }
  line(a, b) {
    return a * this.value + b;
  }
}

addAliases(Foo.prototype, {
  double: 'x2, duplicate',
  line: 'linear'
});

const f = new Foo();

console.log(f.double); // 0
console.log(f.line(1, 2)); // 2

console.log(f.duplicate); // 0
console.log(f.linear(1, 2)); // 2

console.log(f.x2); // 0

Exports

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

Clone this wiki locally