JSLint: 在定义之前使用

嗨,我有3个 javascript 文件。

  • Jquery.js
  • Ulityy.js
  • File1.js

在文件1.js 中我有

jQuery.noConflict()
jQuery(document).ready(function($) {
// ....
});

我得到一个错误“ jQuery”在被定义之前就被使用了。 而“文档”在被定义之前就被使用了。

我如何安全地摆脱这个警告。

如果我愿意的话

var document = document || {};

然后在我的 utility.js 中,如果使用它,它在 IE 中是 null,在 firefox 中是 ok。

解决这个问题的最好办法是什么?

71841 次浏览

From the documentation

JSLint also recognizes a /*global */ directive that can indicate to JSLint that variables used in this file were defined in other files. The comment can contain a comma separated list of names. Each name can optionally be followed by a colon and either true or false, true indicating that the variable may be assigned to by this file, and false indicating that assignment is not allowed (which is the default). The directive respects function scope.

Some globals can be predefined for you. Select the Assume a browser (browser) option to predefine the standard global properties that are supplied by web browsers, such as document and addEventListener.

Example:

/*jslint browser: true*/
/*global $, jQuery*/

As Quentin says, there's a /*global*/ directive.

Here is an example (put this at the top of the file):

/*global var1,var2,var3,var4,var5*/

Make sure the initial global statement is on the same line as /*, or else it breaks.