什么时候javascript错误会破坏其余的代码? [英] When does a javascript error break the rest of the code?

查看:71
本文介绍了什么时候javascript错误会破坏其余的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有时候一个 javascript错误会破坏其他无关的javascript功能,有时则不会。我无法在其中看到一个模式,这使得调试变得困难(特别是当您接受一个旧项目并且没有时间修复所有错误时)。

I've noticed sometimes that one javascript error will break other unrelated javascript functionality and sometimes it doesn't. I couldn't see a pattern in it and this makes debugging difficult (especially when you take on an old project and you don't have time to fix all errors).

例如此代码:

$(".div-one").slick({ // settings for slick plugin });

违反此代码:

$('.product-options').click(function() {
    $('.message').slideToggle('show');
});

没有 .div-one 页面上的元素。它重新调整了元素未定义的错误。所以我把它包装在一个if语句中检查.div-one的长度,现在代码工作正常。

when there was no .div-one element on the page. It retuned an element undefined error. so I wrapped it in an if statement checking for the length of .div-one and now the code works fine.

但很明显有很多情况下javascripts错误不要破坏任何东西。

But obviously there are lots of cases when javascripts error do not break anything.

那么什么时候js错误会导致问题?这两段代码在同一个文件中。也许它只影响同一个文件?

So when will js error cause problems? The 2 pieces of code were in the same file. Maybe it affects only same file?

谢谢!

推荐答案

JavaScript与其他语言没有区别,在这方面有例外。

JavaScript is no different from other languages with exceptions in this regard.

如果你有:

doThis();
doThat();
doSomethingElse();

...和 doThat 抛出异常,它之后的代码(在这种情况下为 doSomethingElse )永远不会运行(所有语言都有异常,包括JavaScript)。

...and doThat throws an exception, the code after it (doSomethingElse, in this case) is never run (in all languages with exceptions, including JavaScript).

所以,如果你有:

$(".div-one").slick({ /* settings for slick plugin */ });

$('.product-options').click(function() {
    $('.message').slideToggle('show');
});

...以及对 slick的调用抛出一个异常,它下面的点击处理程序永远不会被连接,因为该代码不会运行。

...and the call to slick throws an exception, the click handler below it will never get hooked up, because that code doesn't run.

相反,如果 slick 不会抛出异常,但是稍后当你使用任何光滑的功能而它失败时,这不会影响点击处理程序,因为失败并没有阻止获取点击处理程序。

In contrast, if slick doesn't throw an exception, but later when you use whatever the slick functionality is and it fails, that won't affect the click handler, because that failure didn't prevent the click handler from getting hooked up.

与其他有异常的语言一样,您可以控制异常的影响;在JavaScript(和其他几个)的情况下,你用 try / catch (以及可选的 finally )块来做到这一点,例如:

As with other languages with exceptions, you can control the impact of exceptions; in the case of JavaScript (and several others), you do that with try/catch (and optionally finally) blocks, e.g.:

try {
    $(".div-one").slick({ /* settings for slick plugin */ });
} catch (e) {
    // Some appropriate handling of the exception
}
try {
    $('.product-options').click(function() {
        $('.message').slideToggle('show');
    });
} catch (e) {
    // Some appropriate handling of the exception
}

在那里,调用 slick 的异常不会阻止单击处理程序被连接。

There, an exception calling slick doesn't prevent the click handler from being hooked up.

这篇关于什么时候javascript错误会破坏其余的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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