【问题标题】:Javascript Warning: Unexpected mix of '^' and '>>>' no-mixed-operatorsJavascript 警告:“^”和“>>>”非混合运算符的意外混合
【发布时间】:2020-11-08 08:37:02
【问题描述】:

我最近将this hash function 集成到我的 React Web 应用程序中,代码如下:

const cyrb53 = function(str, seed = 0) {
    let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
    for (let i = 0, ch; i < str.length; i++) {
        ch = str.charCodeAt(i);
        h1 = Math.imul(h1 ^ ch, 2654435761);
        h2 = Math.imul(h2 ^ ch, 1597334677);
    }
    h1 = Math.imul(h1 ^ h1>>>16, 2246822507) ^ Math.imul(h2 ^ h2>>>13, 3266489909);
    h2 = Math.imul(h2 ^ h2>>>16, 2246822507) ^ Math.imul(h1 ^ h1>>>13, 3266489909);
    return 4294967296 * (2097151 & h2) + (h1>>>0);
};

我对代码所做的一个更改是我将它放在了一个 utils 文件中,以便该函数可以被多个组件调用。以下是我的细微改动:

export function cyrb53(str, seed = 0) { // <- this is the only change I made to integrate this within my apps architecture
    let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
    for (let i = 0, ch; i < str.length; i++) {
        ch = str.charCodeAt(i);
        h1 = Math.imul(h1 ^ ch, 2654435761);
        h2 = Math.imul(h2 ^ ch, 1597334677);
    }
    h1 = Math.imul(h1 ^ h1>>>16, 2246822507) ^ Math.imul(h2 ^ h2>>>13, 3266489909);
    h2 = Math.imul(h2 ^ h2>>>16, 2246822507) ^ Math.imul(h1 ^ h1>>>13, 3266489909);
    return 4294967296 * (2097151 & h2) + (h1>>>0);
};

我在 Firefox 浏览器控制台中收到以下错误:

  Line 95:23:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 95:27:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 95:61:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 95:65:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 96:23:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 96:27:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 96:61:  Unexpected mix of '^' and '>>>'  no-mixed-operators
  Line 96:65:  Unexpected mix of '^' and '>>>'  no-mixed-operators

查看W3 docs,我知道^&gt;&gt;&gt; 运算符用于字节数学运算。尽管如此,我对javascript中的字节数学并不熟悉。解决此警告的正确方法是什么?

编辑:

接受的答案链接另一个答案,这被标记为重复,但这个主题略有不同。我问,“我的 Javascript 代码(不是 JSX)发生了什么”,答案是“这实际上是一个 eslint 警告——不是 Javascript——这是一个解决方案。”

【问题讨论】:

  • 这能回答你的问题吗? Mixed operators in JSX
  • @CagataySel 不,在那个问题中并不清楚问题不是 javascript 错误。 (那个问题是在讨论 JSX 和不同的操作符。)你的回答更清楚地解决了我的问题,谢谢!

标签: javascript reactjs binary


【解决方案1】:

这不是 javascript 错误,而是 eslint 警告。

请参阅this answer 以消除警告。


带有解决方案的代码(使用括号):

export function cyrb53(str, seed = 0) {
    let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
    for (let i = 0, ch; i < str.length; i++) {
        ch = str.charCodeAt(i);
        h1 = Math.imul(h1 ^ ch, 2654435761);
        h2 = Math.imul(h2 ^ ch, 1597334677);
    }
    h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909); // <- added parentheses here
    h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909); // <- and added parentheses here; its not much but it is honest work
    return 4294967296 * (2097151 & h2) + (h1>>>0);
};

【讨论】:

  • 如果从问答中链接另一个答案回答了问题,那么这个问题应该被投票作为重复关闭,而不是用链接回答。
  • 谢谢!括号解决了这个问题。等待 10 分钟后我会接受,谢谢您的快速解决方案!
  • @PatrickEvans 我从这个答案中学到的一个独特的补充(与链接的答案不同,因为我没有使用 JSX 编写,所以我已经查看并继续前进)是这不是 Javascript 问题.链接的答案中没有像这里那样清楚地概述这一点,所以我认为这是对社区的新贡献。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2019-10-26
  • 2023-03-23
  • 2019-07-04
相关资源
最近更新 更多