Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint: was used before it was defined

Hi I have the 3 javascript files.

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

In file1.js I have

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.

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.

What is the best solution to this?

like image 743
Chun ping Wang Avatar asked Mar 08 '12 16:03

Chun ping Wang


People also ask

Was used before it was defined error?

The "'{a}' was used before it was defined" error (and the alternative "'{a}' is not defined" error) is thrown when JSLint, JSHint and ESLint encounter an identifier that has not been previously declared in a var statement or function declaration.

What does missing use strict statement mean?

The first error pointed out by JSLint is that the "use strict" statement is missing. This error indicates that the function is not executed in strict mode. To correct this error, enable strict mode by adding the following string literal to the beginning of the function body.


2 Answers

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*/ 
like image 200
Quentin Avatar answered Sep 29 '22 19:09

Quentin


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.

like image 22
stanton Avatar answered Sep 29 '22 20:09

stanton