Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created December 10, 2021 12:19
Show Gist options
  • Save rauschma/3b6287a354f41602a56969e1c26bc6a2 to your computer and use it in GitHub Desktop.
Save rauschma/3b6287a354f41602a56969e1c26bc6a2 to your computer and use it in GitHub Desktop.

Cheat sheet: public prototype methods and accessors

const getterKey = Symbol('getterKey');
const setterKey = Symbol('setterKey');
const syncMethodKey = Symbol('syncMethodKey');
const syncGenMethodKey = Symbol('syncGenMethodKey');
const asyncMethodKey = Symbol('asyncMethodKey');
const asyncGenMethodKey = Symbol('asyncGenMethodKey');

class PublicProtoClass2 {
  // Fixed names
  get getter() {}
  set setter(value) {}
  syncMethod() {}
  * syncGeneratorMethod() {}
  async asyncMethod() {}
  async * asyncGeneratorMethod() {}

  // Quoted names
  get 'a getter'() {}
  set 'a setter'(value) {}
  'sync method'() {}
  * 'sync generator method'() {}
  async 'async method'() {}
  async * 'async generator method'() {}

  // Computed names
  get [getterKey]() {}
  set [setterKey](value) {}
  [syncMethodKey]() {}
  * [syncGenMethodKey]() {}
  async [asyncMethodKey]() {}
  async * [asyncGenMethodKey]() {}
}

// Quoted and computed names are accessed via square brackets:
const inst = new PublicProtoClass2();
inst['sync method']();
inst[syncMethodKey]();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment