One var for function in JavaScript?

I use JSLint to make me feel bad in my JavaScript. This is great, by the way. There is one check that I don’t quite understand, and I would like you to take a look at it.

From jslint.com :

In languages ​​with a block area, it is usually recommended to declare variables on the first-use site. But since JavaScript does not have a block area, it is wiser to declare all function variables at the top of the function. It is recommended that you use one var statement for each function.

What is the last bold statement really says? I think I should declare several variables like this?

var foo = 1, bar = 2; 

And, is the “wise” part just a programming style to discourage line errors or is there more to it than that?

Thanks for your help.

+49
javascript lint
Aug 05 '09 at 23:01
source share
4 answers

The problem is, whether you realize it or not, javascript invisibly moves all var declarations to the top of the scope.

so if you have such a function

 var i = 5; function testvar () { alert(i); var i=3; } testvar(); 

the warning window will contain undefined. because internally it has been changed to this:

 var i = 5; function testvar () { var i; alert(i); i=3; } testvar(); 

this is called "rise." The reason crockford protects var declarations so much comes at the top because it makes the code explicitly compatible with what it is going to do, rather than allowing invisible and unexpected behavior.

+71
Aug 05 '09 at 23:27
source share

Basically, JavaScript blocks ( { ... } ) do not introduce a new scope, only a scope exists, so no other scope is created.

A variable entered anywhere in a function is visible everywhere in the function.

For example:

 function myFunction(){ var one = 1; if (one){ var two = one + 1; } (function () { var three = one + two; })(); // at this point both variables *one* and *two* are accessible but // the variable *three* was declared in the scope of a inner function // and is not accessible at this point. } 

In languages ​​with a block area, it is recommended to declare variables at the point of first use, but since JavaScript does not have a block area, it is better to declare all function variables at the top of the function.

Check out the article .

+8
Aug 05 '09 at 23:05
source share

The absence of a block area explains the following code:

 var a = 1; if (something) { var a = 2; } alert(a); // Alerts "2" 

In most C-style languages ​​(as in the syntax), the definition of var a = 2 will define "a" only for the if scope. Using a single var statement at the top of the function avoids this Javascript quirk, which is not always as obvious as above, and would be unexpected for C / C # / Java programmers.

+6
Aug 6 '09 at 2:23
source share

Yes, that means that you declare all the variables at the beginning of the function. If you want to do this on one line or several lines, this is a matter of choice.

The reason is explained in the paragraph you indicate. Javascript variables have only level scope. If you declare the same variable inside the if / while / for block, it will be overwritten with the new value, since the block does not carry a new scope. This is different from languages ​​like Java. To avoid such surprises, declare all the variables that you are going to use in the function at the beginning of the function so that you do not accidentally "redo" everything.

+4
Aug 05 '09 at 23:09
source share



All Articles