评估javascript,检查语法错误 [英] eval javascript, check for syntax error

查看:62
本文介绍了评估javascript,检查语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过javascript查找对eval()的调用是否存在语法错误或未定义的变量等,所以可以说我对某些任意javascript使用eval是否可以捕获该评估的错误输出?

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary javascript is there a way to capture the error output of that eval?

推荐答案

您可以测试以查看错误是否确实是SyntaxError。

You can test to see if an error is indeed a SyntaxError.

try {
    eval(code); 
} catch (e) {
    if (e instanceof SyntaxError) {
        alert(e.message);
    }
}

这篇关于评估javascript,检查语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆