JSHint (derived from JSLint) is a well-known "lint checker" specifically designed for analyzing JavaScript code without executing or altering it. It identifies various potential errors and questionable practices that may be present.
If you place 'use strict';
at the beginning of your JavaScript file, outside any functions, it will enforce strict mode throughout the entire script. By default, JSHint issues a warning upon encountering this syntax.
'use strict';
window.permissions = null;
function initialize() {
window.permissions = 0;
}
Warnings
1: Use the function form of "use strict".
The reason behind this warning is that when multiple JavaScript files are concatenated before delivery to users, having the top-level 'use strict;'
directive can potentially introduce bugs. For instance, if main.js
contains 'use strict';
and is combined with non-strict controls.js
, unintentional strict mode application to the latter's code could alter its behavior.
// This code works independently but might fail under strict mode.
document.querySelector('.upgrade').onclick = function() {
window.permissions = 0777;
}
To prevent such scenarios, avoid placing 'use strict';
at the script's outset. Alternatively, wrap the whole content in a self-invoking function to sidestep concatenation side effects.
(function() {
'use strict';
window.permissions = null;
function initialize() {
window.permissions = 0;
}
}());
If concatenation concerns aren't relevant and code modification isn't desired, using the globalstrict
option in JSHint can suppress this warning. Another possibility is specifying JSHint configurations through a .jshintrc
file or the --config
flag. However, relying on inline configuration via comments within the script often proves to be the simplest approach.
/* jshint globalstrict: true */