Skip to content

JS:eval() 替代方法 #78

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

Open
smileyby opened this issue May 25, 2021 · 0 comments
Open

JS:eval() 替代方法 #78

smileyby opened this issue May 25, 2021 · 0 comments
Labels

Comments

@smileyby
Copy link
Owner

对于规范JSON字符串,通常采用 JSON.parse 解析即可
但对于不规范的,则可采用 eval(str)
eval,jslint检测会告警
解析字符串的前提,字符串必须符合基本的对象格式要求

var str = "{"a":1,"b":2}"; // 规范JSON字符串
var str1 = "{a: 1, b: 2}"; // 不规范JSON字符串

// eval
console.log(eval('(' + str+ ')'));
console.log(eval('(' + str1+ ')'));

// 替代eval
console.log(new Function('return ' + str));
console.log(new Function('return' + str1));

@smileyby smileyby added the JavaScript ☀️ JavaScript label May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant