将函数赋值给变量的值而不是变量名

问题描述

是否有可能将这个函数不分配给变量名 shortcut 而是分配给它的内容

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

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

例如:shortcut 的值可能是一个值为 'abc' 的字符串,我想得到以下结果:

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

解决方法

您可以使用计算属性名称。

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