gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:09:47 25 4
gpt4 key购买 nike

这个标题有点乱码,这可能是重复的,但我已经挖掘了一段时间了。这一定非常简单。关于这个问题的公认答案对我不起作用: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com