JSLint 错误:应为“忽略",而看到的是“ex" [英] JSLint error: Expected 'ignore' and instead saw 'ex'

查看:26
本文介绍了JSLint 错误:应为“忽略",而看到的是“ex"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 JSLint,但就在今天,我遇到了一个我以前从未见过的错误.使用以下代码,我得到如下所示的错误:

I use JSLint all the time, but just today, I came across an error that I've never seen before. With the following code, I got the error shown below:

try {

  document.execCommand('BackgroundImageCache', false, true);

} catch (ex) {}

错误:

Expected 'ignore' and instead saw 'ex'.
} catch (ex) {}

所以我将代码更改为以下内容,错误消失了:

So I changed my code to the following, and the error went away:

try {

  document.execCommand('BackgroundImageCache', false, true);

} catch (ignore) {}

我无法在 Internet 上找到有关为什么这会修复错误的任何解释.有谁知道发生了什么或为什么这解决了问题?

I can't find any explanation on the Internet regarding why this would fix the error. Does anyone know what's up or why this fixed the problem?

谢谢.

推荐答案

我认为你是对的 -- jslint 不习惯这样抱怨.相反,它告诉你你需要对 ex 做一些事情,对吧?稍后我将检查 github 以查看和编辑它.编辑:[由您] 获得了很好的收获!JSLint 的作者 Crockford,在今年 4 月 29 日左右添加了此内容.并且 344>他在 jslint 的源代码中使用了空的 catch 块,所以我猜他也必须给我们一些杂物.;^)

I think you're right -- jslint didn't used to complain like this. Instead, it told you that you needed to do something with ex, right? I'll check github to see and edit this later. EDIT: Great catch [by you]! Crockford, JSLint's author, added this on April 29th of this year around line 3444. And he uses empty catch blocks in jslint's source, so I guess he has to give us the kludge too. ;^)

JSLint 希望您使用 ignore 作为变量名来混合空的 catch 块,以便人们阅读您的代码时很明显故意 不要在 catch 块中做任何事情.正如 Crockford 在别处所说,他觉得,在使用习语时很难编写正确的程序难以与明显错误区分开来."

JSLint wants you to kludge empty catch blocks with ignore as the variable name so that it's obvious to people reading your code that you intentionally meant not to do anything in your catch block. As Crockford says elsewhere, he feels that, "It is difficult to write correct programs while using idioms that are hard to distinguish from obvious errors."

1.) ex 使用(好的)

1.) ex used (okay)

所以(我猜你知道)如果你写了这段代码并且用 ex做一些事情,jslint 不会抱怨.

So (as I'm guessing you know) if you write this code and do something with ex, jslint doesn't complain.

/*jslint browser: true, white:true, sloppy:true*/

var spam;

try {
    spam = "spam";
} catch (ex) {
    window.alert(ex);
}

2.) ex 未使用意味着 ignore(也可以)

2.) ex unused means ignore (okay too)

所以如果你意思不对ex做任何事情,他希望代码告诉人们你没有因为离开你的ex而搞砸code> 通过命名该变量 ignore 在那里等效.

So if you mean not to do anything with ex, he wants the code to tell folks you didn't screw up by leaving your ex equivalent in there by naming that variable ignore.

/*jslint browser: true, white:true, sloppy:true*/

var spam;

try {
    spam = "spam";
} catch (ignore) {
}

3.) ignore used (error!)

3.) ignore used (error!)

因此,在典型的 Crockfordian 方式中,您现在不能使用 ignore 并在 catch 块中实际执行某些操作!

So, in typical Crockfordian fashion, you now can't use ignore and actually do something in the catch block!

/*jslint browser: true, white:true, sloppy:true*/

var spam;

try {
    spam = "spam";
} catch (ignore) {
    window.alert(ignore);
}

这给了

意外的忽略".} catch (ignore) {

Crockford 的另一个超级明确的不要编写看起来像错误的代码"的举动.

Just another hyper-explicit "don't make code that looks like an error" move by Crockford.

(旁白:看看 为什么当您讨论该主题时,空 catch 会阻止一个坏主意.老实说,尽管 ignore 可能是一个有用的约定,基于该讨论(我的意思是,它包括 Skeet 反对空 catch 块!),我有点惊讶 Crockford 允许 ignore 混杂,因为上面的链接的参数感觉很像他调用一些正则表达式不安全"的参数.)

(Aside: Might be fun to take a look at Why are empty catch blocks a bad idea while you're on the subject. Honestly, though ignore is probably a useful convention, based on that discussion (I mean, it includes Skeet arguing against empty catch blocks!), I'm a little surprised Crockford allows the ignore kludge, as the above link has arguments that feel a lot like his for calling some regular expressions "unsafe".)

这篇关于JSLint 错误:应为“忽略",而看到的是“ex"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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