Search code examples
javascriptfunctionkey-bindings

Assigning a function to the value of a variable instead of the variable name


Is it somehow possible to assign this function not to the variable name shortcut but to the content of it?

if (this.commands[this.counter].shortcut) {
  const shortcut = this.commands[this.counter].shortcut;

  tinykeys(window, {
    shortcut: (event) => {
      this.followLink('https://twitter.com');
    },
  });
}

For example: The value of shortcut could be a String with value 'abc' and I want to get the following result:

tinykeys(window, {
  'abc': (event) => {
    this.followLink('https://twitter.com');
  },
});

Solution

  • You can use a computed property name.

    [shortcut]: (event) => {
       this.followLink('https://twitter.com');
    },