I include the statement:
"use strict"; at the beginning of most of my Javascript files.
JSLint has never before warned about this. But now it is, saying:
Use the function form of "use strict".
Does anyone know what the "function form" would be?
Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
Example from that post:
(function () { 'use strict'; // this function is strict... }()); (function () { // but this function is sloppy... }()); Update: In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
For JSLint (per Zhami):
/*jslint node: true */ For JSHint:
/*jshint strict:false */ or (per Laith Shadeed)
/* jshint -W097 */ To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).
Update 2: JSHint supports node:boolean option. See .jshintrc at github.
/* jshint node: true */ -1/*jshint strict:false */, to make it clearer what you're doing (unless there's a particular benefit to your numeric code that I'm not aware of)"use strict"; once per file.If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
/*jslint node: true */ /*jshint strict:false */"node": true to .jshintrcI'd suggest to use jshint instead.
It allows to suppress this warning via /*jshint globalstrict: true*/.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
Otherwise you'd force everyone who is using your library into strict mode.
strict: 'global' now, and see jshint.com/docs/options/#globalstrictI started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
Luckily I found an answer in the Leanpub book on Grunt:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint Running "jshint:all" (jshint) task Linting Gruntfile.js...ERROR [L1:C1] W097: Use the function form of "use strict". 'use strict'; Linting Gruntfile.js...ERROR [L3:C1] W117: 'module' is not defined. module.exports = function (grunt) { Warning: Task "jshint:all" failed. Use --force to continue.Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of
moduleand the string version ofuse strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:jshint: { options: { node: true }, }
Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.
"use strict"; at the top of my JS file, so this might not be entirely true."use strict";, where it's just placed at the top of your code. It only allows "use strict;" when wrapped in a function. (JS_Hint_ allows you to use the global form, though -- see answer above for the setting needed).