javascript - 我如何处理 JSLint 错误和警告?

标签 javascript jquery jslint

我有以下代码:

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

当我使用 jslint 时,它告诉我三件事:

  1. 缺少 'use strict'声明。
  2. 将此与前面的“var”语句结合起来。 <-其中很多
  3. $row: $('#row_' + $link.attr('data-row')) - 错误:“$”在定义之前被使用。

有人可以给我一些关于这些消息的正常做法的建议吗。

最佳答案

  1. 关于use strict,看看strict mode .这是一项可选功能,因此不是错误。

  2. 这只是品味问题。 JSLint 建议你这样写:

    var foo, bar, baz;
    

    代替

    var foo;
    var bar;
    var baz;
    
  3. 这是因为 JSLint 不了解 jQuery(及其“$”变量),因此它认为您正在使用 undefined variable 。你可以在你的 JS 文件的顶部放置一个 /* global $ */ ,或者在文本中输入 $ predefine global variables here(感谢 Fabrício Matté)


此外,关于一般的 JSLint:

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)

关于javascript - 我如何处理 JSLint 错误和警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12458163/

相关文章:

javascript - jQuery 插件,带或不带选择器

jquery - Html.BeginForm 使用 onSubmit 进行验证

javascript - JSLint 意外 'that' 消息

JSLint:我无法在 "var"语句中的空白处安抚它

javascript - JSLint 期望 'new' 而不是看到 '.'

javascript - AngularJS 和 Protractor - 如何检查元素是否显示?

javascript - 如何在 div 中加载页面?

jquery - slider 不显示最后一张图像

javascript - 模态 Bootstrap 4 不显示

JavaScript 发送 GET 请求