Javascript 如果我使用 const,为什么 JSHint 会抛出警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27441803/
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-23 00:09:15  来源:igfitidea点击:

Why does JSHint throw a warning if I am using const?

javascriptnode.jsconstjslintjshint

提问by Andre Schlesinger

This is the error I get when using const:

这是我在使用 const 时遇到的错误:

<error line="2" column="1" severity="warning" message="&apos;const&apos; is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)." source="jshint.W104" />

My code looks like this:

我的代码如下所示:

const Suites = {
    Spade: 1,
    Heart: 2,
    Diamond: 3,
    Club: 4
};

The code works fine only JSHint is warning me every time.

代码工作正常,只有 JSHint 每次都警告我。

回答by James Hibbard

When relying upon ECMAScript 6 features such as const, you should set this option so JSHint doesn't raise unnecessary warnings.

当依赖 ECMAScript 6 特性(例如 )时const,您应该设置此选项,以便 JSHint 不会引发不必要的警告。

/*jshint esnext: true */(Edit 2015.12.29: updated syntax to reflect @Olga's comments)

/*jshint esnext: true */编辑 2015.12.29:更新语法以反映@Olga的评论

/*jshint esversion: 6 */

const Suites = {
    Spade: 1,
    Heart: 2,
    Diamond: 3,
    Club: 4
};

This option, as the name suggests, tells JSHint that your code uses ECMAScript 6 specific syntax. http://jshint.com/docs/options/#esversion

顾名思义,这个选项告诉 JSHint 你的代码使用 ECMAScript 6 特定的语法。 http://jshint.com/docs/options/#esversion

Edit 2017.06.11: added another option based on this answer.

编辑 2017.06.11:根据此答案添加了另一个选项。

While inline configuration works well for an individual file, you can also enable this setting for the entire project by creating a .jshintrcfile in your project's root and adding it there.

虽然内联配置适用于单个文件,但您也可以通过.jshintrc在项目的根目录中创建一个文件并将其添加到其中来为整个项目启用此设置。

{
  "esversion": 6
}

回答by Zanon

You can add a file named .jshintrcin your app's rootwith the following content to apply this setting for the whole solution:

您可以在应用的根目录中添加一个名为.jshintrc的文件,其中包含以下内容,以将此设置应用于整个解决方案

{
    "esversion": 6
}

James' answersuggests that you can add a comment /*jshint esversion: 6 */for eachfile, but it is more work than necessary if you need to control many files.

James 的回答建议您可以/*jshint esversion: 6 */每个文件添加注释,但如果您需要控制多个文件,则工作量会增加。

回答by Nicholas Gentile

I got this same warning when using an export statement. I'm using VS Code and used a similar approach to Wenlong Jiang's solution.

使用导出语句时,我收到了同样的警告。我正在使用 VS Code,并使用了与 Wenlong Jiang 的解决方案类似的方法。

  1. User Settings
  2. JSHint config
  3. "jshint.options": {}(Edit)
  4. Use double quoteswhen specifying "esversion"

    Or copy this snippet into User Settings:

    "jshint.options": {
      "esversion": 6,
    }
    
  1. 用户设置
  2. JSHint 配置
  3. "jshint.options": {}(编辑)
  4. 指定时使用双引号"esversion"

    或将此代码段复制到用户设置中:

    "jshint.options": {
      "esversion": 6,
    }
    

Creating a .jshintrcfile isn't necessary if you want to configure the global jshint settings for your editor

.jshintrc如果要为编辑器配置全局 jshint 设置,则不需要创建文件

回答by Phil

If you're using VSCode:

如果您使用的是 VSCode:

1.

1.

  • Go to preferences-> settings(cmd + ,)
  • Type jshint.optionsinto the search bar
  • Hover over it and click on the pencil icon
  • Its now appended on the right side.
  • Add "esversion": 6to the options object.
  • 转到首选项->设置( cmd + ,)
  • 输入jshint.options到搜索栏
  • 将鼠标悬停在它上面并单击铅笔图标
  • 它现在附加在右侧。
  • 添加"esversion": 6到选项对象。


2.

2.

Or simply add this to your user settings:

或者简单地将其添加到您的用户设置中:

"jshint.options": {
    "esversion": 6
}


[UPDATE] new vscode settings

[更新] 新的 vscode 设置

  • Go to preferences-> settings(cmd + ,)
  • type jshintinto search
  • 转到首选项->设置( cmd + ,)
  • 输入jshint搜索

VSCode Settings

VSCode 设置

  • continue with step 2.
  • 继续步骤 2.

回答by Josh Pittman

I spent ages trying to fix this. Every solution talks about 'setting options'. I don't know what that means. Finally, I figured it out. You can just include a commented out line at the top of the file /*jshint esversion: 6 */.

我花了很长时间试图解决这个问题。每个解决方案都谈到“设置选项”。我不知道那是什么意思。最后,我想通了。您可以只在文件顶部包含注释掉的行/*jshint esversion: 6 */

Solution

解决方案

回答by Wenlong Jiang

You can specify esversion:6inside jshint options object. Please see the image. I am using grunt-contrib-jshint plugin.

您可以在 jshint 选项对象中指定esversion:6。请看图片。我正在使用 grunt-contrib-jshint 插件。

enter image description here

在此处输入图片说明

回答by Prashant Barve

When you start using ECMAScript 6 this error thrown by your IDE.

当您开始使用 ECMAScript 6 时,IDE 会抛出此错误。

There are two options available:

有两种选择:

if you have only one file and want to use the es6 then simply add below line at the top of the file.

如果您只有一个文件并且想要使用 es6,那么只需在文件顶部添加以下行。

/*jshint esversion: 6 */

Or if you have number of js file or you are using any framework(like nodejs express)you can create a new file named .jshintrcin your root directory and add code below in the file:

或者,如果您有多个 js 文件或者您正在使用任何框架(如 nodejs express),您可以.jshintrc在根目录中创建一个名为的新文件,并在文件中添加以下代码:

{
    "esversion": 6
}

If you want to use the es6 version onward for each project you can configure your IDE.

如果你想为每个项目使用 es6 版本,你可以配置你的 IDE。

回答by Singhak

In your package.json you can tell Jshint to use es6 like this

在你的 package.json 你可以告诉 Jshint 像这样使用 es6

"jshintConfig":{
    "esversion": 6 
}

回答by Sudhanshu Gaur

If you are using Webstorm and if you don't have your own config file, then just enable EcmaScript.nextin Relaxing options in in

如果您使用的是 Webstorm 并且没有自己的配置文件,那么只需EcmaScript.next

Settings | Languages & Frameworks | JavaScript | Code Quality Tools | JSHint

设置 | 语言和框架 | JavaScript | 代码质量工具 | JSHint

See this question How-do-I-resolve-these-JSHint-ES6-errors

看到这个问题How-do-I-resolve-these-JSHint-ES6-errors

回答by Sridhar

If you are using Grunt configuration, You need to do the following steps

如果您使用的是 Grunt 配置,则需要执行以下步骤

Warning message in Jshint:

Jshint 中的警告消息:

enter image description here

在此处输入图片说明

Solution:

解决方案:

  1. Set the jshint options and map the .jshintrc.js file
  1. 设置 jshint 选项并映射 .jshintrc.js 文件

enter image description here

在此处输入图片说明

  1. Create the .jshintrc.js file in that file add the following code
  1. 在该文件中创建 .jshintrc.js 文件添加以下代码
{  
  "esversion": 6  
} 

After configured this, Run again It will skip the warning,

配置好后,再次运行它会跳过警告,

enter image description here

在此处输入图片说明