Javascript Jshint.com 要求“严格使用”。这是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8107000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:45:33  来源:igfitidea点击:

Jshint.com requires "use strict". What does this mean?

javascriptjshint

提问by

Jshint.com is giving the error:

Jshint.com 给出了错误:

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

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

采纳答案by Czarek Tomczak

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

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

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

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

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

JavaScript 中的“use strict”有什么作用,背后的原因是什么?

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 有问题,它要求您在每个函数中放置“use strict”,但应该允许为每个文件全局设置它。

jshint.com thinks this is wrong.

jshint.com 认为这是错误的。

"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.

做得好!JSHint 没有发现您的代码有任何问题。

回答by Anton Kovalyov

JSHint maintainer here.

JSHint 维护者在这里。

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(网站上使用的版本)要求您在代码中使用函数级严格模式。关闭它很容易,您只需要取消选中“代码未处于严格模式时发出警告”复选框:

jshint.com screenshot

jshint.com 截图

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.

为什么我们不允许@Czarek 建议的全局严格模式?因为您页面上使用的某些 JavaScript 文件可能与严格模式不兼容,而全局严格模式会破坏该代码。要使用全局严格模式,有一个名为globalstrict.

Hope that helps!

希望有帮助!

回答by Kornel Dylski

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.

我认为这是因为 jshint 试图“保护”我们免受对整个文件的意外分配严格模式。而且最好用匿名函数包装代码,或者使用某种命名空间。

e.g. both function in strict mode:

例如,两者都在严格模式下运行:

(function() {

   "use strict";

   function foo() {
        .....
   }

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

回答by MikeD

JSlint requires your code to be in 'strict mode'

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

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

要做到这一点,只需添加"use strict";到代码的顶部。