代码之家  ›  专栏  ›  技术社区  ›  Noob

为什么在这行代码中使用分组运算符?

  •  0
  • Noob  · 技术社区  · 4 年前

    我正在经历 express-session npm打包源代码并运行在以下代码行中:

    // get the session ID from the cookie
    var cookieId = (req.sessionID = getcookie(req, name, secrets));
    

    不是和这条线一样吗?:

    // get the session ID from the cookie
    var cookieId = req.sessionID = getcookie(req, name, secrets);
    

    他们为什么要这么做?这是某种代码风格的决定还是我错过了什么?谢谢。

    2 回复  |  直到 4 年前
        1
  •  1
  •   Aplet123    4 年前

    如果不在 = 表情,由于人们通常打字 = 当他们想打字的时候 == . 表达式周围的括号告诉林特这是一个有意的决定。否则,这两行在语义上是等价的。

        2
  •  0
  •   Samathingamajig    4 年前

    似乎是文体上的选择,特别是因为它清楚地表明 = 是故意的,不是 == ===

    foo = bar = 5;
    faz = (baz = 5);
    
    console.log({foo, bar});
    console.log({faz, baz});