83

Jshint.com 给出错误:

第 36 行:var signin_found;缺少“使用严格”声明。

4

4 回答 4

40

在 js 文件的顶部(在 .js 文件的第 1 行)添加“use strict”:

"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) 
{

在 stackoverflow 上的另一个问题中更多关于“使用严格”的信息:

“use strict”在 JavaScript 中做了什么,背后的原因是什么?

更新。

jshint.com 有问题,它要求您在每个函数中添加“use strict”,但应该允许为每个文件全局设置它。

jshint.com 认为这是错误的。

"use strict";    
function asd()
{
}

但这并没有什么不妥……

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.

于 2011-11-12T19:46:15.900 回答
34

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:

jshint.com screenshot

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!

于 2011-11-12T23:05:11.827 回答
11

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() {
        .....
   }
}());
于 2013-07-24T13:20:19.457 回答
5

JSlint 要求您的代码处于“严格模式”

为此,只需添加"use strict";到代码的顶部。

于 2011-11-12T19:37:00.270 回答