Javascript eval 多个变量不起作用
我得到了一个数组,我对其进行迭代并尝试创建一个变量。
变量的名称是variable,来自数组。所以我使用 eval (此代码仅在我的本地计算机上使用)来创建变量。 奇怪的是,我可以创建一个变量并向其内容添加纯文本。但每当我尝试设置一个变量时,我什么也得不到。
我还使用 Prototype 轻松遍历 DOM。
var arr_entries = some_DOM_element;
arr_entries_array = new Array();
arr_entries_array[0] = new Array();
arr_entries_array[0][0] = 'name_dd';
arr_entries_array[0][1] = arr_entries.next(13).down().next(1).innerHTML;
arr_entries_array[1] = new Array();
arr_entries_array[1][0] = 'name_pl';
arr_entries_array[1][1] = arr_entries.next(14).down().next().innerHTML;
arr_entries_array[2] = new Array();
arr_entries_array[2][0] = 'name_pm';
arr_entries_array[2][1] = arr_entries.next(15).down().next().innerHTML;
arr_entries_array[3] = new Array();
arr_entries_array[3][0] = 'name_hd';
arr_entries_array[3][1] = arr_entries.next(17).down().next().innerHTML;
arr_entries_array[4] = new Array();
arr_entries_array[4][0] = 'name_sr';
arr_entries_array[4][1] = arr_entries.next(16).down().next().innerHTML;
for(e = 0; e < arr_entries_array.length; e++)
{
eval('var arr_entry_' + arr_entries_array[e][0] + ';');
eval('arr_entry_' + arr_entries_array[e][0] + ' = \'' + arr_entries_array[e][1] + '\';');
}
我可以提醒(arr_entries_array[e][1]
)就好了。我还可以用纯文本替换它,然后提醒变量,它就会起作用。
第二个评估行是哪里出错了,有什么意见吗?
I got an array which I iterate over and try to create a variable.
The name of the variable is variable and comes from the array. So I am using eval (this code will only be used on my local computer) to create the variables.
Weird enough I can create a variable and add plain text to the contents of it. But whenever I try to set a variable variable I get nothing.
I'm also using Prototype to easily walk the DOM.
var arr_entries = some_DOM_element;
arr_entries_array = new Array();
arr_entries_array[0] = new Array();
arr_entries_array[0][0] = 'name_dd';
arr_entries_array[0][1] = arr_entries.next(13).down().next(1).innerHTML;
arr_entries_array[1] = new Array();
arr_entries_array[1][0] = 'name_pl';
arr_entries_array[1][1] = arr_entries.next(14).down().next().innerHTML;
arr_entries_array[2] = new Array();
arr_entries_array[2][0] = 'name_pm';
arr_entries_array[2][1] = arr_entries.next(15).down().next().innerHTML;
arr_entries_array[3] = new Array();
arr_entries_array[3][0] = 'name_hd';
arr_entries_array[3][1] = arr_entries.next(17).down().next().innerHTML;
arr_entries_array[4] = new Array();
arr_entries_array[4][0] = 'name_sr';
arr_entries_array[4][1] = arr_entries.next(16).down().next().innerHTML;
for(e = 0; e < arr_entries_array.length; e++)
{
eval('var arr_entry_' + arr_entries_array[e][0] + ';');
eval('arr_entry_' + arr_entries_array[e][0] + ' = \'' + arr_entries_array[e][1] + '\';');
}
I can alert(arr_entries_array[e][1]
) just fine. I can also replace it with plain text, alert the variable afterwards and it will work.
The second eval line is where it goes wrong, any comments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接在
Object
上设置属性呢?如果您发现自己在代码中编写了代码,然后使用
eval()
执行它,那么您几乎肯定会做错事情。它速度慢、难以阅读,并且会引入安全漏洞。JavaScript 对象可以具有您想要的任何属性。为什么不只是
v = new Object(); v['name_dd'] = 随便...;
,或者类似的东西?Why not just set properties on
Object
?If you find yourself writing code in your code and then executing it with
eval()
you are almost certainly going about things incorrectly. It's slow, hard to read, and introduces security holes.JavaScript objects can have any properties you want. Why not just
v = new Object(); v['name_dd'] = whatever...;
, or something like that?根据这些 Dom 元素的 innerHTML 值,您可能会在 eval 中遇到语法错误。
尝试使用 firebug 或其他调试工具来找出第二个 eval 中出现的错误消息,您可能会找到罪魁祸首。
我的钱在新行 (
\n
) 上,这是问题所在。要解决此问题,您需要正确转义传递给 eval 的值,使它们成为您在 eval 语句中使用的文字字符串。例如
Depending on what the innerHTML values of those Dom elements are, you may be getting syntax errors inside the eval.
Try using firebug or some other debugging tool to figure out what the error message is coming from the second eval and you will probably find your culprit.
My money is on new lines (
\n
) being the issue.To fix the problem, you need to properly escape the values being passed into eval such that they are literal strings which you appear to use them as inside the eval statement. e.g.
我建议寻找另一种方法。即使您仅在自己的计算机上运行代码并且不会暴露安全违规行为,但从 eval 到公共 eval 仍然是错误的并且效率极低。我建议您永远不要使用 eval,因为它可能是不可预测的,而且任何其他解决方案可能会更有效地处理。
I recommend finding another method. Even though you are running the code only on your own computer and not exposing security violations, from eval, to the public eval is still faulty and extremely inefficient. I recommend that you never use eval because it can be unpredictable and because any other solution likely to be more efficient to process.