javascript try catch在Firefox中无法完全正常工作 [英] javascript try catch not fully working in firefox

查看:94
本文介绍了javascript try catch在Firefox中无法完全正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么:

Can someone explain me why this:

<html>
<head></head>
<body>
    <script>
        try {
            document.attribute-withminus = 5;
        }
        catch(e) {
            alert('something went wrong');
        }

        alert('ok');
    </script>
</body>
</html>

既不向我发出出问题了"的警报,也不向我发出好"的警报?

Doesn't give me an alert with 'something went wrong' and also no alert with 'ok'?

它在chrome中可以正常工作.但是在firefox中,它只是退出(它确实在Web控制台中显示错误). try-catch的全部目的是确保如果我键入错误,它应该给我一个警告.我不想一直打开Web控制台.

It works fine in chrome. But in firefox, it just exits (it does show an error in the web console). The whole point of that try-catch is to make sure that if I type something wrong, it should give me an alert saying so. I don't want to have the web console open all the time.

此外,我知道这里出了什么问题(减号属性;应使用setAttribute).我在问为什么我的错误没有被发现.

Also, I know what is wrong here (minus sign in attribute; should use setAttribute). I'm asking why my error wasn't caught.

推荐答案

document.attribute-withminus = 5;

是一个语法错误(可能类似于分配中的左手无效部分" ),它是一个

is a syntax error (probably something like "invalid left hand part in assignement") which is an early error, not a runtime error.

浏览器不应执行包含该脚本的脚本,它应在编译包含错误的代码之前立即停止并报告该错误,然后再进行评估.在大多数浏览器中,脚本将在到达try子句之前被完全编译.它可以在Chrome中运行,因为Chrome会延迟编译,直到需要内部块为止.

The browser isn't supposed to execute the script containing it, it's supposed to stop and report the error as soon as it compiles the code containing the error, prior to any evaluation. In most browsers, the script would be wholly compiled before reaching the try clause. It works in Chrome because Chrome delays the compilation until it needs the internal block.

这篇关于javascript try catch在Firefox中无法完全正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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