【发布时间】:2011-09-13 13:11:39
【问题描述】:
我正在从 Smarty 传递一些 JSON 数据。我将其应用于 JavaScript 变量 options。
如果你看过我之前关于日期格式的问题,你就会知道我需要对传入的数据做一些工作,所以我有一个名为 chart_convert_dates() 的函数,它被调用,传入选项(好吧,options.data),然后在它返回时将其重新设置。
如果您通读我的代码,您会注意到我正在调试选项变量,它与原来的在调用函数之前发生了变化!?
如果我注释掉函数调用,则变量不会被触及,因为它应该在那个时候。
Chrome、FF 都会出现这种情况……这是怎么回事?
{literal}
<script type="text/javascript">
$(document).ready(function() {
// set JS var, this data is coming in from smarty
var options = {/literal}{$options}{literal};
// these should both be exactly the same
debug.debug({/literal}{$options}{literal});
debug.debug(options);
// but the above outputs aren't the same! options has been modified
// by the function below... that hasn't even fired yet!? We can prove
// this by commenting out the following function call
options.data = chart_convert_dates(options.data);
// ... do something else
});
</script>
{/literal}
【问题讨论】:
-
生成的代码是什么?也许你可以看到那里的警告。
-
如果使用 alert 而不是 debug.debug 会发生什么?是否设置了断点并检查了值?
-
@Johannes 我已经放了一个断点,并且在函数之前没有更改变量......正如马特指出的那样,
debug.debug一定是一个问题。谢谢:-)
标签: javascript jquery variables