-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 10 期(2019-05-17):JSON.parse方法polyfill #12
Comments
if (!window.JSON) {
window.JSON = {
parse: function(sJSON) { return eval('(' + sJSON + ')'); },
stringify: (function () {
var toString = Object.prototype.toString;
var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
return function stringify(value) {
if (value == null) {
return 'null';
} else if (typeof value === 'number') {
return isFinite(value) ? value.toString() : 'null';
} else if (typeof value === 'boolean') {
return value.toString();
} else if (typeof value === 'object') {
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON());
} else if (isArray(value)) {
var res = '[';
for (var i = 0; i < value.length; i++)
res += (i ? ', ' : '') + stringify(value[i]);
return res + ']';
} else if (toString.call(value) === '[object Object]') {
var tmp = [];
for (var k in value) {
if (value.hasOwnProperty(k))
tmp.push(stringify(k) + ': ' + stringify(value[k]));
}
return '{' + tmp.join(', ') + '}';
}
}
return '"' + value.toString().replace(escRE, escFunc) + '"';
};
})()
};
} 虽然这是一道送分题,作为一直小白还是有点小疑惑请教 //对于
eval('(' + sJSON + ')');
//在浏览器控制台里
var json={"a":"b","c":"d"}
eval(json);
{a: "b", c: "d"}//正常
var json={"a":"b","c":"d"}
eval("("+json+")");//报错
VM191:1 Uncaught SyntaxError: Unexpected identifier
at <anonymous>:2:14
(anonymous) @ VM190:2
var json={"a":"b","c":"d"}
eval('('+json+')');//报错
VM205:1 Uncaught SyntaxError: Unexpected identifier
at <anonymous>:2:14
(anonymous) @ VM204:2 这是为毛啊? |
@AMY-Y 这题以及上述参考答案我是在 codewars 上看到的,我作答时用的也是 eval。 |
这下通了,之前困扰了我很久,感谢大神 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
为
JSON.parse()
方法写一个 polyfill,使其可以兼容低版本浏览器(IE8)参考答案:
本期优秀回答者: @AMY-Y
The text was updated successfully, but these errors were encountered: