我从Smarty传递了一些JSON数据。我正在将其应用于JavaScript变量options
。奇怪的JavaScript行为,变量函数被调用前的变量值变化
如果您已经看到我之前关于日期格式的问题,您将知道我需要对数据进行一些工作,所以我有一个名为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}
什么是生成的代码?也许你可以看到那里的警告。 – pimvdb
如果使用alert而不是debug.debug,会发生什么情况?你是否设置了断点并检查了值? –
@Johannes我已经放了一个断点,并且该变量在函数之前没有被改变...必须是Matt指出的'debug.debug'的问题。谢谢:-) –