Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint is suddenly reporting: Use the function form of "use strict"

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?

like image 918
Zhami Avatar asked Dec 16 '10 15:12

Zhami


People also ask

What is the function form of use strict?

“use strict”; is a string literal expression place on the first line of the javascript file or the first line in a javascript function. This line will be read and enforced by ECMAScript version 5(javascript 1.8. 5) or newer, and ignored by older versions of javascript.

What is the use of JSLint?

JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It is provided primarily as a browser-based web application accessible through the domain jslint.com, but there are also command-line adaptations.

What is the use strict in JavaScript?

The "use strict" Directive The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive.

Do JavaScript modules automatically use strict mode?

Strict mode for modulesThe entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.


1 Answers

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 */ 
like image 62
bdukes Avatar answered Oct 31 '22 22:10

bdukes