Jshint.com 要求“严格使用”。这意味着什么?

发布于 2024-12-15 01:34:43 字数 101 浏览 0 评论 0原文

Jshint.com 给出错误:

第 36 行:varsignin_found;缺少“use strict”声明。

Jshint.com is giving the error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-12-22 01:34:43

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

"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”的信息:

“use strict”在 JavaScript 中做什么,背后的推理是什么是吗?

更新。

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

jshint.com 认为这是错误的。

"use strict";    
function asd()
{
}

但它没有任何问题......

它希望你对每个函数添加“use strict”:

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

然后它说:

干得好! JSHint 尚未发现您的代码有任何问题。

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.

别念他 2024-12-22 01:34:43

这里是 JSHint 维护者。

JSHint(网站上使用的版本)要求您在代码中使用函数级严格模式。关闭它非常容易,您只需取消选中“代码不处于严格模式时发出警告”复选框:

jshint. com Screenshot

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

希望有帮助!

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!

昵称有卵用 2024-12-22 01:34:43

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

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

(function() {

   "use strict";

   function foo() {
        .....
   }

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

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() {
        .....
   }
}());
聊慰 2024-12-22 01:34:43

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

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

JSlint requires your code to be in 'strict mode'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文