开发者

JSLint - problems declaring variables

The following code passes JSLint:

var sGreeting = 'hello world';

switch (sGreeting)
{
case 'Hello world!':
    var a = 'some a value';
    break;
case 'Kamusta mundo!':
    var b = 'some b value';
    break;
case 'Salut le Monde!':
    var c = 'some c value';
    break;
default:
    break;
}

However, once I put that code in a function, JSLint complains that I should Combine ... with the previous 'var' statement. If I follow JSLint, I would be defining variables that may never need to be used. How should I deal with this problem? Here's the code followed by JSLint's errors:

function foo()
{
    'use strict';
    var sGreeting = 'he开发者_JS百科llo world';

    switch (sGreeting)
    {
    case 'Hello world!':
        var a = 'some a value';
        break;
    case 'Kamusta mundo!':
        var b = 'some b value';
        break;
    case 'Salut le Monde!':
        var c = 'some c value';
        break;
    default:
        break;
    }
}


Error:
Problem at line 9 character 7: Combine this with the previous 'var' statement.
var a = 'some a value';

Problem at line 12 character 7: Combine this with the previous 'var' statement.
var b = 'some b value';

Problem at line 15 character 7: Combine this with the previous 'var' statement.
var c = 'some c value';


The only scope in javascript is functional scope (braces do not provide scope), so those vars in the switch statement are being hoisted to the top of the function.

What jslint is suggesting is this:

function foo() {
    'use strict';
    var a, b, c, sGreeting;

    sGreeting = 'hello world';

    switch (sGreeting) {
    case 'Hello world!':
        a = 'some a value';
        break;
    case 'Kamusta mundo!':
        b = 'some b value';
        break;
    case 'Salut le Monde!':
        c = 'some c value';
        break;
    default:
        break;
    }
}


"If I follow JSLint, I would be defining variables that may never need to be used."

If you do it your way and ignore JSLint you would still be defining variables that may never be used.

The reason for this is that JavaScript treats all var declarations inside a function as if they happened at the top of the function, even if you thought you were declaring the variable(s) inside some conditional logic such as inside a specific case (or if or for or whatever). This is called "hoisting". The actual values then get assigned to the variables at the point in the code where you did your assignment. That is, the "hoisted" variables get an undefined value initially, and then at the point in the code where you had your var a = "something"; the value will be assigned.

So as the other answers said, you can get your code to pass JSLint by declaring the variables at the top of the function (comma separated with a single var statement), and then assign the values at whatever point you like.


To avoid this JSLint errors you may

  1. Define all variables together

    var sGreeting = 'hello world', a, b, c;

  2. Use JSLint directive Tolerate many var statements per function


The reason for this is that JavaScript does not have block scope; it has globals and locals only. JSLint does not care about global variables being declared anywhere, but for local variables the story is different. This is because a local variable declared anywhere in the middle of a function is actually available everywhere inside the function.

You should place var a, b, c at the top of the function, and then assign to them in your switch statement.

As to your concern about defining variables that may never be used, that is not really a problem in JavaScript. In a way you are really only declaring, not defining. Simply writing var a, b, c; brings the three variables into existence with the value undefined. In fact, the way you wrote your code, you get the same effect! Any local variable you define with var in the middle of the function is implicitly declared and set to undefined at the top of the function body anyway. It is considered by many to be good programming practice to make this declaration explicit.


Use the vars option to quietly allow this usage of var.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜