JSLint and the “Expected to see a statement and instead saw a block.” error

怎甘沉沦 提交于 2019-12-06 14:44:25

Good question.

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/Statements/switch

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?

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!