javascript - How to call a function using variable in javascript without using window?
问题描述
Suppose there is a function (from a module) named fiveStar
and I want to call it using a variable,
For e.g using window
, I could do this:
const num = 'five';
const functionName = `${num}Star';
window[functionName]();
Is there any other way I could do this without using window
?
解决方案
You could use an object to group functions together.
const myModule = {
fiveStar: ()=>{}
//other methods...
};
const num = 'five';
const functionName = `${num}Star`;
myModule[functionName]();
推荐阅读
- winapi - 通过 CryptAPI 生成并添加到远程证书存储的自签名证书没有私钥
- python - 如何匹配csv文件中数据库中的值?
- sql - 我想选择它们在我的多边形之外的所有点
- uwp - UWP C#:如何使框架元素变为全窗口模式
- heroku - heroku.com 上的链接断开:主机不回答
- javascript - 如何在 apache2 VPS 的子域上托管 Node.js 应用程序?
- python - 如何在cmd中创建django项目
- python - 用于输入和输出的 Swig Python 类型映射
- r - 基于R dataf中字符串中的单词过滤数据
- getstream-io - 发送与 Emoji 按钮重叠的按钮(React 聊天组件)