JSLint, else and Expected exactly one space between '}' and 'else' error

社会主义新天地 提交于 2019-11-28 12:04:37

JSLint is based on Crockford's preferences (which I share in this case).

It's a matter of opinion which is "better".

(Although clearly his opinion is right ;)

It's not a matter of style. It's how ECMAScript works.

For better or for worse, it will automatically insert semicolons at the end of statements where it feels necessary.

JavaScript would interpret this:

function someFunc {
    return
    {
        something: 'My Value'
    };
}

As this:

function someFunc {
    return;
    {
        something: 'My Value'
    };
}

Which is certainly what you don't want.

If you always put the bracket on the same line as the if and if else statement, you won't run into a problem like this.

As with any coding language, the coding style chosen should be the one that minimizes potential risk the most.

Mozilla Developer Network also promotes same line bracketing: https://developer.mozilla.org/en-US/docs/User:GavinSharp_JS_Style_Guidelines#Brackets

JSLint is being very picky here, just enforcing a style that you might not share.

Try JSHint instead:

The project originally started as an effort to make a more configurable version of JSLint—the one that doesn't enforce one particular coding style on its users [...]

JSLint is just being picky here. The guy who wrote it also baked in many stylistic suggestions in order to keep his own code more consistent.

As for semicolon insertion, you shouldn't need to worry here. Inserting a semicolon before the else clause would lead to a syntax error and automatic semicolon insertion only occurs in situations where the resulting code would still be syntactically valid.

If you want to read more on semicolon insertion, I recommend this nice reference

Basically if you insert semicolons everywhere you only need be careful about putting the argument to "return" or "throw" (or the label for "break" and "continue") on the same line.

And when you accidentally forget a semicolon, the only common cases that are likely to bite you are if you start the next line with an array literal (it might parsed as the subscript operator) or a parenthsised expression (it might be parsed as a function call)

Conclusion

Should you omit optional semicolons or not? The answer is a matter of personal preference, but should be made on the basis of informed choice rather than nebulous fears of unknown syntactical traps or nonexistent browser bugs. If you remember the rules given here, you are equipped to make your own choices, and to read any JavaScript easily.

If you choose to omit semicolons where possible, my advice is to insert them immediately before the opening parenthesis or square bracket in any statement that begins with one of those tokens, or any which begins with one of the arithmetic operator tokens "/", "+", or "-" if you should happen to write such a statement.

Whether you omit semicolons or not, you must remember the restricted productions (return, break, continue, throw, and the postfix increment and decrement operators), and you should feel free to use linebreaks everywhere else to improve the readability of your code.


By the way, I personally think that the } else { version is prettier. Stop insisting in your evil ways and joins us on the light side of the force :P

StonedRanger

I have just finished reading a book titled Mastering JavaScript High Performance. I speak under correction here, but from what I can gather is that "white space" does in fact matter.

It has to do with the way the interpreter fetches the next function. By keeping white space to a minimum (i.e.) using a minifier when your code is ready for deployment, you actually speed up the process.

If the interpreter has to search through the white space to find the next statement this takes time. Perhaps you want to test this with a piece of code that runs a loop say 10,000 times with white space in it and then the same code minified.

The statement to put before the start of the loop would be console.time and finally console.timeEnd at the end of the loop. This will then tell you how many milliseconds the loop took to compute.

The JSLint error/warning is suggesting to alter code to

// naming convention winner? it's subjective
} else if{
    b = 2;
}

from:

}
else if{
 b = 2;
}

It prevents insert semicolons; considered more standard & conventional. most people could agree a tab between the }tabelse if{

is not the most popular method. Interesting how the opening bracket { is placed (space or not) obviously both ar subjected

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