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

时间:2014-10-15 19:22:22

标签: javascript json

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

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

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

1 个答案:

答案 0 :(得分:1)

您无法将函数打包为JSON。

_userObj['137340'].effect

只计算一个你尝试传递为delta的字符串(" aniFx.ease.swingTo"),然后在执行aniFx.move时尝试将其作为函数调用。