尝试将多个变量分配给同一值时,ESLint引发错误 [英] ESLint throws an error when trying to assign multiple variables to the same value

查看:55
本文介绍了尝试将多个变量分配给同一值时,ESLint引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么ESLint在此代码行的 a 变量声明之后抛出解析错误:意外的令牌?

const a,b,c = 1;

我的 .eslintrc.json 看起来像这样;

  {"env":{浏览器":是的,"es6":是的,"jquery":是的,"commonjs":为true},扩展":[以"airbnb为基础",漂亮"],"parserOptions":{"ecmaVersion":6"sourceType":脚本"},"plugins":["prettier"],规则":{更漂亮/更漂亮":错误",半":[错误",总是"],"quotes":[错误",双精度"]}} 

解决方案

您无法在JS中做到这一点.

如果变量声明中列出了多个变量( var let const ),则每个值都有其自己的初始化程序,因此在没有重复的情况下,不可能在单个声明语句中为多个变量分配单个值.

如果缺少初始化程序(不存在 = value 部分),则变量将设置为 undefined .

因此,如果您使用 let ,则 c 将变为 1 ,而 a b undefined :

 让a,b,c = 1;console.log(a,b,c)//未定义未定义1  

但是,在 const 声明中省略初始化程序会引发错误(将 undefined 分配给常量没有太大意义,对吧?)

因此,您的代码将失败(不仅在ESLint中,而且在正确实现ECMAScript标准的任何JS运行时中):

  const a,b,c = 1;//SyntaxError  

要将相同的值分配给多个变量,您必须:

  • 重复该值(如果该值是对象文字(包括数组文字和函数表达式)或由构造函数,工厂或非纯函数返回,则将无效:

      const a = 1,b = 1,c = 1;console.log(a,b,c)//1 1 1  

  • 互相分配变量(提示:将名称最短的变量放在第一位并重复该变量):

      const a = 1,b = a,c = a;console.log(a,b,c)//1 1 1  

  • 使用 let :

     让a,b,c;a = b = c = 1console.log(a,b,c)//1 1 1  

  • 解构一个无限迭代器(在大量变量的情况下似乎很有用):

      const [a,b,c] =(function *(v){while(true)yield v})(1);//^ ---值console.log(a,b,c)//1 1 1  

Why does ESLint throw a Parsing error: Unexpected token , after a variable declaration on this line of code?

const a, b, c = 1;

My .eslintrc.json looks like this;

{

    "env": {
        "browser": true,
        "es6": true,
        "jquery": true,
        "commonjs": true
    },
    "extends": [
        "airbnb-base",
        "prettier"
    ],
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "script"
    },
    "plugins": ["prettier"],
    "rules": {
        "prettier/prettier": "error",
        "semi": ["error", "always"],
        "quotes": ["error", "double"]
    }

}

解决方案

You cannot do that in JS.

If multiple variables listed in a variable declaration (either var, let or const), each value has its own initializer, so it's impossible to assign a single value to multiple variables in a single declaration statement, without repetition.

If an initializer is missing (no = value part present), the variable will be set to undefined.

So if you used let, then c would become 1, while a and b would be undefined:

let a, b, c = 1;

console.log(a, b, c) //undefined undefined 1

However, omitting the initializer in a const declaration throws an error (it doesn't make too much sense to assign undefined to a constant, right?)

Therefore, your code fails (not just in ESLint, but in any JS runtime that correctly implements the ECMAScript standard):

const a, b, c = 1; //SyntaxError

To assign the same value to multiple variables, you have to either:

  • Repeat the value (this won't work if the value is an object literal (including array literals and function expressions) or returned by a constructor, a factory, or non-pure function):

    const a = 1, b = 1, c = 1;
    
    console.log(a, b, c) //1 1 1

  • Assign the variables from each other (tip: put the one with the shortest name first and repeat that one):

    const a = 1, b = a, c = a;
    
    console.log(a, b, c) //1 1 1

  • Use let:

    let a, b, c;
    a = b = c = 1
    
    console.log(a, b, c) //1 1 1

  • Destructure an infinite iterator (that seems useful in case of a very large number of variables):

    const [a, b, c] = (function*(v){while(true) yield v})(1);
                                                       // ^--- Value
    
    console.log(a, b, c) //1 1 1

这篇关于尝试将多个变量分配给同一值时,ESLint引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆