【发布时间】:2011-10-23 05:24:00
【问题描述】:
function (data) {
if (typeof data !== "string" || !data) {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim(data);
// Attempt to parse using the native JSON parser first
if (window.JSON && window.JSON.parse) {
return window.JSON.parse(data);
}
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if (rvalidchars.test(data.replace(rvalidescape, "@").replace(rvalidtokens, "]").replace(rvalidbraces, ""))) {
return (new Function("return " + data))();
}
jQuery.error("Invalid JSON: " + data);
}
我无法理解以下后备方案
return (new Function("return " + data))();
还有(这个不在jQuery中)
return (eval('('+ data + ')')
我想知道这些事情
- 这种解析回退的实际工作原理是什么?
- 为什么在回退中不使用 eval? (是不是比
new Function()快)
【问题讨论】:
标签: javascript jquery json parsing