Question

I am trying out JSLint on some of the javascript files in our projects at work and have come across several errors like the following:

JS Lint: '<var name>' is not defined

The issue is that the variable is defined in a separate file that is referenced elsewhere. e.g. the HTML page has global.js and pageSpecific.js

I am aware using the syntax

/* global varName */ 

to tell JSLint that yes infact this variable does exist but this is not ideal in this scenario due to the number of different variables causing these errors (there would be lines of these globals at the top of every page). Nor do I just want to ignore this error, after all maybe there is a 'genuine' undefined error there that needs some attention.

My feeling is that either there is a JSLint solution I am unaware of or, perhaps more likely, that is indicative of a problem with how the JS is structured in this project? Are 'global.js' files (of this sort) a bad practice?

Ideas/feedback appreciated.

Was it helpful?

Solution

It is indicative of a problem with how the JS is structured. Globals should be kept to a minimum. Things that really need to be globally available should all hang off a single global object (this technique is known as Namespacing in the JS world).

OTHER TIPS

You can also pass a list of globals into the command you use to invoke JSLint/JSHint, though the greater issue is you shouldn't have that many variables polluting the global scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top