3

I have added the following line as the FIRST line in my javascript file:

'use strict';

My understanding was that by adding this as the first line, ALL code following is placed in a "strict" operating context-- which makes sense...

However, I run JSLint and it reports 71 warnings, i.e.:

[JSLint] Recommendation: Expected 'use strict' before 'var'. Visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode for more information.

Basically, it looks like it wants to see 'use strict'; in every function??

So, what is the purpose of including "use strict" in the first line if (according to JSLint), every function in the file needs it?

Let's just change the syntax of all javascript statements look something like:

"use strict"; for ( var i = 0; i < 1000; i++ ) {
"use strict";     console.log( 'this is dumb' );
"use strict";   }

Why is this directive needed in nearly every function?

I am at a loss.


I've read the marked duplicate JSLint is suddenly reporting: Use the function form of "use strict" and do not believe this is a duplicate of that question.

According to everything I have read and what I have read in the marked duplicate -- the directive should apply to EVERY line of code in the file -- can someone please explain why this is NOT the case when using JSLint?

Community
  • 1
  • 1
bdcoder
  • 3,280
  • 8
  • 35
  • 55
  • Could you post your JS file please? Or at least more lines from it (begging, end and line with error) – Kiril Mar 11 '16 at 17:59
  • As a note, if you wrap all your code in an IIFE you can put the `"use strict";` directive at the top of the IIFE and won't have to duplicate it across every sub-function because they'll be within the strict scope. This may pose problems if you need to expose additional globals, but that can generally be handled by adding explicit globals across the file as needed. – zzzzBov Mar 11 '16 at 18:04
  • If you'd read the first line of the accepted answer to the linked question it answers your questions: "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." – zzzzBov Mar 11 '16 at 18:05
  • So when building javascript modules -- what is the best practice to use a single "use strict"; directive per module? I've tried the "wrapping function" examples in the previous link and JSLint STILL reports a HUGE number of "strict" warnings? – bdcoder Mar 11 '16 at 18:06
  • @user1377587, that's a primarily opinion based question, and has a lot of context to make the right decision. Generally, wrap all your code in an IIFE and put `"use strict";` at the top of the IIFE. If you're using browserify, or any other CommonJS module loader you'll want to switch the `node: true` flag on for JSLint. – zzzzBov Mar 11 '16 at 18:08
  • Yes -- that works for module patterns -- but when using the module_name.prototype.fn_name = function () {} syntax -- it appears JSLint wants a "use strict" in every function -- Ugh! – bdcoder Mar 11 '16 at 18:17

0 Answers0