JSLint - problems declaring variables

浪子不回头ぞ 提交于 2019-12-10 01:52:30

问题


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 = '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;
    }
}


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';

回答1:


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



回答2:


"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.




回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/7357736/jslint-problems-declaring-variables

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