JSLint Validation error “combine this with the previous var statement”

后端 未结 2 726
一个人的身影
一个人的身影 2020-12-11 16:14

JSLint Validation error \"combine this with the previous var statement\"

How do I combine this so I don\'t get JSLint Validation error? I get the validation error on

相关标签:
2条回答
  • 2020-12-11 17:16

    This error means that you have multiple var statements in some of your functions, such as:

    var x = 1;
    var y = 2;
    

    JSLint wants you to combine the variable declarations in a single var statement like:

    var x = 1,
        y = 2;
    
    0 讨论(0)
  • 2020-12-11 17:18

    I think JSLint is referring to these few lines of your code (towards the bottom of the code you have provided):

    var userEnteredDate = new Date($('#dateOfLastAccident').val()); // variable that stores user entered date
    var today = new Date(); 
    var minutes = 1000 * 60; // calculation used to convert miliseconds to minutes
    var hours = minutes * 60; // calculation used to conver minutes into hours
    var days = hours * 24; // calculation used to convert hours to days
    var years = days * 365; // calculation used to convert days into years
    var daysSinceAccident = Math.floor((today.getTime() - userEnteredDate.getTime()) / days); // calculation used to find the difference between current date and user entered date as a whole number
    var className = getClassName(daysSinceAccident); // variable clasName finds the correct css style to apply based on daysSinceAccident
    

    You can avoid declaring them multiple times by delimiting each variable with a comma:

    var userEnteredDate = new Date($('#dateOfLastAccident').val()),
        today = new Date(),
        minutes = 1000 * 60,
        hours = minutes * 60,
        days = hours * 24,
        years = days * 36,
        daysSinceAccident = Math.floor((today.getTime() - userEnteredDate.getTime()) / days),
        className = getClassName(daysSinceAccident);
    
    0 讨论(0)
提交回复
热议问题