JSLint:在定义之前使用过

时间:2022-11-05 17:14:45

Hi I have the 3 javascript files.

嗨,我有3个javascript文件。

  • jquery.js
  • 的jquery.js
  • utility.js
  • utility.js
  • file1.js
  • file1.js

In file1.js I have

在file1.js我有

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

I get an error 'jQuery' was used before it was defined. and 'document' was used before it was defined.

我得到一个错误'jQuery'在定义之前就被使用了。并且'文档'在定义之前使用。

How do I safely get rid of this warning.

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

If I do

如果我做

var document = document || {}; 

then in my utility.js if it is used, it would be null in IE and ok in firefox.

然后在我的utility.js中如果使用它,它将在IE中为null,在Firefox中为ok。

What is the best solution to this?

这是什么最好的解决方案?

2 个解决方案

#1


167  

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.

JSLint还识别一个/ * global * /指令,该指令可以向JSLint指示此文件中使用的变量是在其他文件中定义的。注释可以包含逗号分隔的名称列表。每个名称后面可以跟冒号后跟true或false,true表示该变量可以由该文件赋值,false表示不允许赋值(默认值为)。该指令尊重功能范围。

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.

可以为您预定义一些全局变量。选择“假定浏览器(浏览器)”选项以预定义Web浏览器提供的标准全局属性,例如document和addEventListener。

Example:

例:

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

#2


29  

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

正如Quentin所说,有一个/ * global * /指令。

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.

确保初始全局语句与/ *在同一行,否则它会中断。

#1


167  

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.

JSLint还识别一个/ * global * /指令,该指令可以向JSLint指示此文件中使用的变量是在其他文件中定义的。注释可以包含逗号分隔的名称列表。每个名称后面可以跟冒号后跟true或false,true表示该变量可以由该文件赋值,false表示不允许赋值(默认值为)。该指令尊重功能范围。

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.

可以为您预定义一些全局变量。选择“假定浏览器(浏览器)”选项以预定义Web浏览器提供的标准全局属性,例如document和addEventListener。

Example:

例:

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

#2


29  

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

正如Quentin所说,有一个/ * global * /指令。

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.

确保初始全局语句与/ *在同一行,否则它会中断。