gpt4 book ai didi

javascript - 从 JSON 对象解析设置导致字符串不是函数

转载 作者:行者123 更新时间:2023-12-03 11:38:14 26 4
gpt4 key购买 nike

所以我为我的项目提供了这个 JSON 对象。它包含多个设置,其中一些是字符串,一些是 bool 值。

但是我无法执行一件事,即从存储在变量中的对象添加动画名称。

aniFx.move(inner, {
duration: 1,
delta: aniFx.ease.bouncePast
});

说明

Inner: document.getElementById('inner');
Duration: time multiplied by 1000 in my animation script (aniFx)
Delta: Animation used for moving the inner element

现在我已经解释过了,它工作得很好,直到我尝试从 JSON 对象设置 delta 。为了解决这个问题,假设我的 JSON 对象仅包含以下内容:

_userObj = JSON.parse('{ "137340": { "effect": "aniFx.ease.swingTo" } }');
<小时/>

现在,为什么我无法做到这一点:

aniFx.move(inner, {
duration: 1,
delta: _userObj['137340'].effect
});

当我触发该函数时,我会收到以下错误...

控制台将返回:

console.log => aniFx.ease.swingTo
aniFx.move => Uncaught TypeError: string is not a function

最佳答案

您不能将函数打包为 JSON。

_userObj['137340'].effect

计算结果为您尝试作为增量传递的字符串(“aniFx.ease.swingTo”),然后在执行 aniFx.move 时尝试将其作为函数调用。

关于javascript - 从 JSON 对象解析设置导致字符串不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26390447/

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