【发布时间】:2014-05-30 08:28:54
【问题描述】:
evaling 用函数声明数组的字符串可以正常工作。但是,evaling 用函数声明对象的字符串不起作用。谁能告诉我为什么?
This JSFiddle 和下面的代码演示了这个问题:
"use strict";
// functions in arrays are ok:
var x = "[ function() {}, [function() {}] ]";
var o = eval(x);
console.log(o[1]);
// functions in objects are not ok (for some reason):
var y = "{a: function() {}, b: [function() {}] }";
var o = eval(y);
console.log(o['a']);
只有第二个eval 会引起麻烦。
在 Chrome 中,我得到:
未捕获的 SyntaxError:在严格模式代码中,函数只能在顶层或立即在另一个函数中声明。
在 IE 11 中,我得到:
预期标识符
【问题讨论】:
标签: javascript eval strict