admin管理员组

文章数量:1314496

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.

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.

Share Improve this question edited Sep 17, 2012 at 11:41 fresskoma 25.8k12 gold badges86 silver badges131 bronze badges asked Sep 17, 2012 at 11:21 user1464139user1464139
Add a ment  | 

4 Answers 4

Reset to default 7
  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)

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.

1) use strict: This basically makes you write better JavaScript. It prevents you from using some of the "bad" features in JavaScript.

2) Combine var: Don't worry about this one, it's just a style preference.

3) $ was used before it was defined: it's just informing you that $ doesn't exist. You have to add it to the jsLint global variables.

You can disable any of these options within jsLint by putting the options at the start of your JS file:

/*jslint options */

More info at http://www.jslint./lint.html

  1. Add "use strict"; to the start to the function.
  2. Declare the variables using one var statement: var foo = b, bar = d, etc.
  3. Add /*global $: false */ to the start of the file. This will tell JSLint that there exists a global variable called $ which the script can use.

本文标签: javascriptHow do I deal with JSLint errors and warningsStack Overflow