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

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

问题描述

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

  try {

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

} catch(ex){}

错误:

 预期忽略,而是看到ex。 
} catch(ex){}

所以我把我的代码改成了以下,错误消失了:

  try {

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

} catch(ignore){}

我找不到任何互联网上的解释为什么会修复错误。有没有人知道发生了什么或为什么这样解决问题?



谢谢。

解决方案

我认为你是对的 - jslint没有这样抱怨。相反,它告诉你,你需要用 ex 做某事,对吧?我会检查github,以便稍后查看和编辑。 编辑:很好的抓住[你] Crockford,JSLint的作者,在今年4月29日添加了这个 3444。他在jslint的来源中使用空的 catch 块,所以我猜他也必须给我们kludge。 ; ^)



JSLint希望您使用 ignore catch c $ c>作为变量名,所以很明显,阅读你有意义的代码的人不会在你的 catch 块中做任何事情。作为Crockford 其他地方,他觉得使用惯用语难以编写正确的程序这很难区别于明显的错误。



1。) ex used(okay) / strong>



所以(正如我猜你知道的),如果你写这段代码,并用 ex ,jslint不会抱怨。

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

var spam;

尝试{
spam =spam;
} catch(ex){
window.alert(ex);
}

2。) ex unused意味着 ignore (好的)



所以如果你的意思是 c $ c> ex 不要做任何事情,他希望代码告诉你不要把你的 ex 等价于该变量忽略

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

var spam;

尝试{
spam =spam;
} catch(忽略){
}

3) ignore used(error!)



所以,以典型的Crockfordian时尚,不能使用 ignore ,并在 catch 块中执行某些操作!

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

var spam;

尝试{
spam =spam;
} catch(ignore){
window.alert(ignore);
}

这给了


意外的忽略。 } catch(ignore){


只是另一个超显式don t代码看起来像一个错误由Crockford移动。



(Aside:看看为什么是空的catch块是一个坏主意,而你在这个问题上老实说,虽然忽略可能是一个有用的约定,基于这个讨论(我的意思是它包括Skeet争论反对空的 catch 块!),有点惊讶的Crockford允许忽略 kludge,因为上面的链接有一些参数,感觉很像他调用一些正则表达式不安全。)


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) {}

Error:

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) {}

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?

Thank you.

解决方案

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 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 used (okay)

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 unused means ignore (okay too)

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!)

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);
}

That gives

Unexpected 'ignore'. } catch (ignore) {

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

(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天全站免登陆