JSLint和“期望看到一条语句,而是看到了一个块".错误 [英] JSLint and the "Expected to see a statement and instead saw a block." error

查看:70
本文介绍了JSLint和“期望看到一条语句,而是看到了一个块".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于

I have picked up the habit of wrapping all of my case statements in curly brackets from programming in C because of this but JSLint is throwing a fit. It stops validating at that point.

我的问题是:这在JS中是一种不好的做法吗?我是否不必担心范围问题,因为JS具有函数范围(我知道情况会怎样,我只是想一个很好的理由,以免对此保持一致")?

My question is: Is this such a bad practice in JS? Do I not have to worry about the scope issue because JS has function scope (I understand how that would be the case, I just want a good reason not to be 'consistent' on this)?

(我知道不同的语言需要不同的做法,但是我正在努力使各种语言尽可能地保持一致,以帮助保护自己的理智.)

(I know that different languages call for different practices, but i am trying to be as consisten as possible across languages to help protect my sanity.)

推荐答案

好问题.

JSLint抱怨这一点的原因是因为它实际上与switch/case语句的语言规范相反:

The reason that JSLint complains about this is because it is actually contrary to the language specification for the switch/case statments:

http://www.ecma-international. org/publications/files/ECMA-ST/ECMA-262.pdf

https://developer.mozilla.org/en/JavaScript/Reference/语句/开关

JavaScript编译器会容忍它,但是JSLint旨在确保您的代码遵循JavaScript的更严格和更正确的子集. (道格拉斯·克罗克福德(Douglas Crockford)所说的好人"!)

The JavaScript compier will tolerate it, however JSLint is about ensuring your code adheres to a stricter and more correct subset of JavaScript. ("The Good Bits" as Douglas Crockford puts it!)

此外,额外的支撑是您必须在网站上传送的额外字符.如果您不需要它们,为什么还要传输它们?

Moreover, the extra bracing is extra characters that you will have to transmit with the website. If you don't need them, why transmit them?

请参阅上面的链接,在JavaScript中不会出现相同的问题.因此,以下方法将起作用:

Referring to your above link, the same problem does not present itself in JavaScript. So, the following will work:

var x = 0;
switch(x){
    case 0:
        var y = 1;
        alert(y);
}

在此JSFiddle上查看: http://jsfiddle.net/LKWwB/

See it at this JSFiddle: http://jsfiddle.net/LKWwB/

最后,关于您的理智,我将放弃您对此的脆弱控制.我几年前做过,为此感到非常高兴:-)

Finally, regarding your sanity, I would relinquish the tenuous grip you have on it. I did years ago and am much happier for it :-)

这篇关于JSLint和“期望看到一条语句,而是看到了一个块".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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