jshint:'use the function form of "use strict"' 和 'document is not defined' 在简单的 javascript 文件中

jshint: 'use the function form of "use strict"' and 'document is not defined' in simple javascript file

我将它们捆绑在一起是因为我认为它们是相关的。 最简单的简单代码:

'use strict';
const x = document.querySelector('#score strong');

结果如下

"use the function form of use strict (W097)"
"document is not defined (W117)"

这可能是错误或警告; W 表示警告,但我不知道如何确定。

那么,另一个问题:这些警告或错误是什么,我该如何判断自己?这是在哪里引用的?

我正在使用 Atom 1.31,我认为是 JSHint(不管是什么——我对这一切都是陌生的)。我正在使用 ES6 - .jshintrc:

{
"esversion": 6
}

我应该如何在全局范围内指定 use strict?将它放在一个函数中以便全局使用意味着,呃,将我的脚本的全部内容放在一个函数中。不?是吗?

还有我如何规避这个文件没有定义的东西呢? 我试过了

const document=this.document;
const document=global.document;
const document=window.document;

所有结果为 warnings/errors(随便)。

所以,明确地说,我的问题是:

  1. 这些是警告还是错误,我该如何判断?

  2. 我如何并且确实需要规避使用严格的事情?

  3. 我该怎么做,我确实需要,绕过文件没有定义的东西吗?

您需要设置 strict option to prefer a global 'use strict', and the browser option 来告诉 JSHint 您的脚本针对浏览器。

.jshintrc

{
    "esversion": 6,
    "browser": true,
    "strict": "global"
}

是的,代码开头的“W”表示“警告”。