The following JS:
(function() {
"use strict";
$("#target").click(function(){
console.log("clicked");
});
}());
Yields:
test.js: line 5, col 3, '$' is not defined.
When linted using JSHint 0.5.5. Any ideas?
The following JS:
(function() {
"use strict";
$("#target").click(function(){
console.log("clicked");
});
}());
Yields:
test.js: line 5, col 3, '$' is not defined.
When linted using JSHint 0.5.5. Any ideas?
To fix this error when using the online JSHint implementation:
If you're using an IntelliJ editor, under
You can type in anything, for instance
console:false
, and it will add that to the list (.jshintrc) as well - as a global.If you're using an IntelliJ editor such as WebStorm, PyCharm, RubyMine, or IntelliJ IDEA:
In the Environments section of File/Settings/JavaScript/Code Quality Tools/JSHint, click on the jQuery checkbox.
Instead of recommending the usual "turn off the JSHint globals", I recommend using the module pattern to fix this problem. It keeps your code "contained" and gives a performance boost (based on Paul Irish's "10 things I learned about Jquery").
I tend to write my module patterns like this:
You can get these other performance benefits (explained more here):
window
object declaration gets minified as well. e.g.window.alert()
becomem.alert()
.window
object.window
property or method, preventing expensive traversal of the scope chain e.g.window.alert()
(faster) versusalert()
(slower) performance.All you need to do is set
"jquery": true
in your.jshintrc
.Per the JSHint options reference:
If you are using a relatively recent version of JSHint, the generally preferred approach is to create a .jshintrc file in the root of your project, and put this config in it:
This declares to JSHint that $ is a global variable, and the false indicates that it should not be overridden.
The .jshintrc file was not supported in really old versions of JSHint (such as v0.5.5 like the original question in 2012). If you cannot or do not want to use the .jshintrc file, you can add this at the top of the script file:
There is also a shorthand "jquery" jshint option as seen on the JSHint options page..