javascript - 首选解构修复不起作用

标签 javascript ecmascript-6 eslint

<分区>

相关:How to fix Eslint error "prefer-destructuring"? .

我有:

method (v100) => {
    let L,
        H;
    if (v100 <= 70) {
        const obj = {L: 1, H: 2};
        L = obj.L;
        H = obj.H;
    } else {
        L = ((0.8353 * (v100 ** 2)) + (14.67 * v100)) - 216;
        H = ((0.1684 * (v100 ** 2)) + (11.85 * v100)) - 97;
    }
    return ((L - 40) / (L - H)) * 100
}

并将其更改为

{ L } = obj;
{ H } = obj;

如答案中所写。但现在我收到意外的 token 错误。

这样写应该也可以吧?:

{ L, H } = obj;

最佳答案

尝试:

({ L, H } = obj);

引自MDN web docs

The round braces ( ... ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.

{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1, b: 2}

NOTE: Your ( ... ) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.

关于javascript - 首选解构修复不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656318/

相关文章:

javascript - 循环对象数组,并根据某些条件生成新数组

javascript - 让 eslint 与 eslint-plugin-vue 一起工作时出错

javascript - 数组中每个元素的变量

javascript - 是否有任何方法可以将对象唯一地推送到数组?

javascript取消图像加载

javascript - 使用 Promise.all 执行数组中的 promise 会破坏它们

javascript - 期望一个赋值或函数调用,而是看到一个 expression.eslint no-unused-expressions

javascript - 如何从 npm 脚本运行 eslint --fix

javascript - 如何在 javascript 中实现对 webSocket 连接的 Ping/Pong 请求?

javascript - ng-if AngularJS中的日期比较不是年份