Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deal with JSLint errors and warnings?

I have the following code:

var $form = $modal.find('#main-form');
var $submitBt = $modal.find('.block-footer button:contains("Submit")');
var oSubmit = {
    $form: $form,
    $modal: $modal,
    action: $form.attr('data-action'),
    entity: $form.attr('data-entity'),
    href: $form.attr('data-href'),
    row: $link.attr('data-row'),
    $row: $('#row_' + $link.attr('data-row')),
    $submitBt: $submitBt
};

When I used jslint it told me three things:

  1. Missing 'use strict' statement.
  2. Combine this with the previous 'var' statement. <- many of these
  3. $row: $('#row_' + $link.attr('data-row')) - error: '$' was used before it was defined.

Can someone give me some advice on what's normal practice with these messages.


2 Answers

  1. Regarding use strict, take a look at strict mode. It is an opt-in feature, so it is not an error.

  2. This is simply a matter of taste. JSLint propose that you write:

    var foo, bar, baz;
    

    instead of

    var foo;
    var bar;
    var baz;
    
  3. This is because JSLint doesn't know about jQuery (and its "$" variable), so it thinks you are using undefined variables. You may put a /* global $ */ at the top of your JS file, or type $ into the textare that says predefine global variables here (thanks Fabrício Matté)


Also, regarding JSLint in general:

JSLint tests one particular person's (Douglas Crockford) opinions regarding what makes good JavaScript code. Crockford is very good, but some of his opinions are anal retentive at best, like the underscore rule, or the use of the increment/decrement operators.

Many of the issues being tagged by JSLint in the above output are issues that Crockford feels leads to difficult to maintain code, or they are things that he feels has led him to doing 'clever' things in the past that can be hard to maintain.

Source (foxxtrot)

like image 160
fresskoma Avatar answered Dec 07 '25 21:12

fresskoma


They are just messages, not errors. You can easily switch them off and/or ignore them.

1) is more a tip than a "missing statement".

2) is a code style hint. You may write:

var $form = …,
    $submitBt = …,
    oSubmit = …;

3) seems like a unusual inclusion of jQuery (did you redeclare it?), or that jslint missed the global variable.

like image 38
Bergi Avatar answered Dec 07 '25 20:12

Bergi