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

时间:2021-01-11 17:25:19

标签: javascript function key-bindings

是否有可能将这个函数不分配给变量名 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');
  },
});

1 个答案:

答案 0 :(得分:3)

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

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