What is the reason behind JSLint saying there are “too many var statements”

后端 未结 7 1108
时光说笑
时光说笑 2020-12-13 12:06

JSLint (with the onevar flag turned on) is flagging some javascript code that I have with the following:

Problem at line 5 character 15: Too many var statements.

        
相关标签:
7条回答
  • 2020-12-13 12:57

    The reasoning is already described.

    Recommendation is to use this form:

    var myVar1 = document.getElementById("myDiv1"),
      myVar2 = document.getElementById("myDiv2");
    

    or this:

    var myVar1, myVar2;
    myVar1 = document.getElementById("myDiv1");
    myVar2 = document.getElementById("myDiv2");
    

    But this doesn't look very nice, especially if you want to document vars.

    So you can just disable this warning temporarly:

      /*jslint vars: true*/
      /**
       * @returns {HTMLDivElement}
       */
      var myVar1 = document.getElementById("myDiv1");
      /**
       * @returns {HTMLDivElement}
       */
      var myVar2 = document.getElementById("myDiv2");
      /*jslint vars: false*/
    

    Warning: make sure that this is done at the top of a function.

    I think this is done because jslint couldn't reliably determine if vars were declared at the top of function or not.

    0 讨论(0)
提交回复
热议问题