javascript - 将函数的名称存储在 JSON 文件中,然后能够从脚本中加载和调用它?

标签 javascript electron vis.js

这个标题有点乱码,这可能是重复的,但我已经挖掘了一段时间了。这一定非常简单。关于这个问题的公认答案对我不起作用:How to declare and use the name of a function from a json object?

任务:我正在尝试将 Vis.js 时间轴的设置数据外部化到 JSON 文件中。除了函数引用“orderByID”和“visTemplate”之外,数据集没有问题,所有选项也没有问题。这些是我定义的函数,它们存在于我正在处理 JSON 数据的脚本中。

当我尝试使用 JSON 而不尝试转换它时,Vis.js 会提示。当我使用下面的代码尝试上述问题的答案时,我得到了图像中显示的错误。

这是在 Electron 中,脚本是通过 index.html 中的脚本标签加载的。

我等待这个简单问题的一行答案,这个问题已经花了很多时间来描述。 😉

<小时/>
console.log(' timelineOptions.order', timelineOptions.order);
console.log(' timelineOptions.template', timelineOptions.template);
console.log('this', this);
console.log('window', window);

timelineOptions.order = window[timelineOptions.orderByID];
timelineOptions.template = window[timelineOptions.visTemplate];
<小时/>

enter image description here

<小时/>
 "timelineOptions": {
    "order": "orderByID",
    "selectable": true,
    "zoomable": false,
    "width": "100%",
    "height": "90%",
    "minHeight": 700,
    "format": {
        "minorLabels": {
            "hour": "HH\\h"
        }
    },
    "margin": {
        "axis": 20,
        "item": 20
    },
    "start": "2016-12-30",
    "end": "2017-01-4",
    "template": "visTemplate",
    "showCurrentTime": false,
    "dataAttributes": "all",
    "timeAxis": { "scale": "day", "step": 1 },
    "orientation": {
        "axis": "top",
        "item": "top"
    }
}

最佳答案

不确定您是否在窗口对象上设置了正确的引用,但您的代码不应该读取:

timelineOptions.order = window[timelineOptions.order];

您引用了字符串值orderByID,而不是用于设置对象的属性名称。

关于javascript - 将函数的名称存储在 JSON 文件中,然后能够从脚本中加载和调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864471/

相关文章:

node.js - Node - Electron -FS(保存/读取)使 Electron 崩溃

javascript - 如何在 vis.js 时间轴中对嵌套组进行排序?

javascript - 将 HammerJS 与 jQuery 结合使用

javascript - JS 测试运行器 html 与生产 html?

javascript - 让div在滚动时淡入淡出

php - 启动 Electron 应用程序并从网络浏览器(Chrome、Firefox)向其发送数据

javascript - 使用 Three.js 绘制尺寸线和 3D 立方体

node.js - Electron 生产构建 - ProtocolDeprecateCallback : The callback argument of protocol module APIs is no longer needed

javascript - 最小/最大和开始/结束不能一起工作

javascript - 使用 vis.js 更改作为节点的图像颜色