Jshint.com requires “use strict”. What does this m

2019-01-22 16:25发布

问题:

This question already has an answer here:

  • What does “use strict” do in JavaScript, and what is the reasoning behind it? 25 answers

Jshint.com is giving the error:

Line 36: var signin_found; Missing "use strict" statement.

回答1:

Add "use strict" at the top of your js file (at line 1 of your .js file):

"use strict";
...
function initialize_page()
{
    var signin_found;
    /*Used to determine which page is loaded / reloaded*/
    signin_found=document.getElementById('signin_button');
    if(signin_found) 
{

More about "use strict" in another question here on stackoverflow:

What does "use strict" do in JavaScript, and what is the reasoning behind it?

UPDATE.

There is something wrong with jshint.com, it requires you to put "use strict" inside each function, but it should be allowed to set it globally for each file.

jshint.com thinks this is wrong.

"use strict";    
function asd()
{
}

But there is nothing wrong with it...

It wants you to put "use strict" to each function:

function asd()
{
    "use strict";
}
function blabla()
{
    "use strict";
}

Then it says:

Good job! JSHint hasn't found any problems with your code.



回答2:

JSHint maintainer here.

JSHint—the version used on the website—requires you to use function-level strict mode in your code. It is very easy to turn that off, you just need to uncheck "Warn when code is not in strict mode" checkbox:

Why don't we allow global strict mode as suggested by @Czarek? Because some of the JavaScript files used on your page might not me strict mode compatible and global strict mode will break that code. To use global strict mode, there is an option called globalstrict.

Hope that helps!



回答3:

I think its because jshint is trying to "protect" us against accidental assignment strict mode to entire file. And also it is good to wrap code with anonymous function, or use somekind of namespace.

e.g. both function in strict mode:

(function() {

   "use strict";

   function foo() {
        .....
   }

   function bar() {
        .....
   }
}());


回答4:

JSlint requires your code to be in 'strict mode'

To do this simply add "use strict"; to the top of your code.