JavaScript formatter and comma separated vars [message #715805] |
Mon, 15 August 2011 11:45  |
Eclipse User |
|
|
|
I was wondering whether there's a way (I can't find one) of getting the JavaScript formatter to line-break after commas in multiple-variable declarations?
Currently:
var myVar = 'test', hisVar = 'real', herVar = 'live';
Ideally:
var myVar = 'test',
hisVar = 'real',
herVar = 'live';
This would then conform with the standard JSLint practice of declaring all at the front, and fit in nicely with the JavaScript parser's predilection for grabbing all the variables in the function and then simply declaring them first anyway!
If there's no way of doing it, how would I go about putting it in as a feature request?
|
|
|
Re: JavaScript formatter and comma separated vars [message #716317 is a reply to message #715805] |
Tue, 16 August 2011 23:09   |
Eclipse User |
|
|
|
On 8/15/2011 11:45 AM, Martin wrote:
> I was wondering whether there's a way (I can't find one) of getting the
> JavaScript formatter to line-break after commas in multiple-variable
> declarations?
>
> Currently:
>
> var myVar = 'test', hisVar = 'real', herVar = 'live';
>
> Ideally:
>
> var myVar = 'test',
> hisVar = 'real',
> herVar = 'live';
>
> This would then conform with the standard JSLint practice of declaring
> all at the front, and fit in nicely with the JavaScript parser's
> predilection for grabbing all the variables in the function and then
> simply declaring them first anyway!
>
> If there's no way of doing it, how would I go about putting it in as a
> feature request?
You can enter a feature request here:
https://bugs.eclipse.org/bugs/enter_bug.cgi?classification=WebTools&product=JSDT&component=General&bug_severity=enhancement
Although that particular combination, one var keyword with multiple
lines, doesn't seem like it would be that popular.
--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational
|
|
|
Re: JavaScript formatter and comma separated vars [message #716481 is a reply to message #716317] |
Wed, 17 August 2011 10:26   |
Eclipse User |
|
|
|
Thank you for that Nitin. The reason for it is to do with JavaScript's scoping. I'll just quote the infamous Douglas Crockford, instead of explaining myself:
Quote:In many languages, a block introduces a scope. Variables introduced in a block are not visible outside of the block.
In JavaScript, blocks do not introduce a scope. There is only function-scope. A variable introduced anywhere in a function is visible everywhere in the function. JavaScript's blocks confuse experienced programmers and lead to errors because the familiar syntax makes a false promise.
[...]
In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function. This can be declined with the vars option.
Hence why people prefer to have a single var, with multiple lines to make it more readable. Plenty of discussion on StackOverflow on a similar theme, if anyone's interested, e.g. use bit.ly/oza1yZ (can't post links yet)
Anyway, I'll go off and make that feature request when I get some time
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04391 seconds