javascript - 将函数分配给变量的值而不是变量名
问题描述
是否有可能将此函数分配给变量名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');
},
推荐阅读
- django - Django 将 FileField 中的文件保存在 docker 容器中
- r - 几何文本样式错误
- css - 在渲染 Child 时更改 Parent 样式
- python - 将 Alpha 通道添加到 RGB 图像
- c# - 处理已创建表单的关闭按键 [C#]
- python - 在 python 中使用 None 值对列表进行排名
- javascript - 根据字体大小动态更改文本阴影 xy 偏移量
- javascript - 使用函数 Javascript/React 中的字符串动态调用函数/变量
- r - 清理李克特量表数据:除了一些不相关的字符串,如何测试数据是否连续?
- swift - CoreData insert(object: NSManagedObject) 函数的目的是什么?