您可能未使用的运算符

    科技2025-03-14  23

    If you do a google search for the term javascript operators and click on the first result— this mystical operator will be nowhere to be seen. At least in September of 2020, you have to click on the fourth result and then do a search on the page for it, otherwise, it is really easy to overlook it as it is not included in any of the pretty tables.

    如果您在Google中搜索术语javascript operators ,然后单击第一个结果-这个神秘的运算符将无处可见。 至少在2020年9月,您必须单击第四个 结果,然后在页面上进行搜索,否则,很容易忽略它,因为它没有包含在任何漂亮的表格中。

    足够的秘密! (Enough secrets!)

    I’m talking about the Nullish Coalescing Operator — ?? , it is a binary logical operator, the same as || (logical OR)and && (logical AND) except that it does not do type coercion, in practice it is similar to || and the syntax is as follows:

    我说的是Nullish Coalescing Operator - ?? ,它是一个二进制逻辑运算符,与||相同(逻辑OR)和&& (逻辑AND),只是不进行强制转换,实际上与||类似。 语法如下:

    leftExp ?? rightExp

    leftExp ?? rightExp

    The expression will return rightExp if leftExp is equal to null or undefined , no truthy or falsy conversion taking place with unforeseen consequences, like as 0 failing our number check as it is falsy

    如果leftExp等于null或undefined ,则表达式将返回rightExp ,不进行任何truthy或falsy转换,不会发生无法预料的后果,例如0因为我们的falsy我们的数字检查失败

    It seems that we can replace || with ?? and call it a day then?

    看来我们可以代替|| 与?? 叫一天呢?

    不,这就是为什么 (No, here’s why)

    First and foremost, this operator is quite new, Chrome only supports it from version 80, which came out just in February of 2020, and it would require Node v14 (April of 2020)on the server-side of JavaScript things.

    首先,该运算符是一个相当新的东西,Chrome仅从2020年2月发布的80版本开始支持它,并且在JavaScript事物的服务器端需要Node v14(2020年4月)。

    browser support 浏览器支持

    Secondly nesting an expression with ?? will still do type conversion, a great example is an if statement:

    其次,用??嵌套一个表达式仍然会进行类型转换,一个很好的例子是if语句:

    if(0 ?? -1) { console.log("a number!")}

    The log will never execute, we will get 0 from the expression, but in the boolean context of the if it will still be considered falsy .

    日志将永远不会执行,我们将从表达式中获得0 ,但在boolean上下文中, if仍认为falsy 。

    Lastly, when chaining it with other logical operators, its return value will still be subject to type conversion which makes it harder to mentally process what is really going on in. It requires an extra set of parenthesis as well:

    最后,当将其与其他逻辑运算符链接时,其返回值仍将受到类型转换的约束,这使得在心理上很难真正处理正在发生的事情。它还需要附加一组括号:

    let a = true && (0 ?? false); // 0let b = (0 ?? "bar") || "foo"; // "foo"let c = null ?? "bar" ?? "foo"; // "bar"let d = (0 ?? "foo") && "bar"; // 0

    虽然不是很糟糕 (It’s not all bad though)

    You can shorten the checks for edge cases in assignment operations with it nicely, no need to explicitly check for null and undefined which should be done in the case of the logical OR.

    您可以很好地缩短分配操作中对边缘情况的检查,而无需显式检查null和undefined ,这在逻辑或的情况下应进行。

    let a = 0;let b = a ?? -1;let c = 0;let d = c !== null && c !== undefined ? c : -1;

    Looking at the previous example, the chaining with the logical OR makes no sense, replacing it with ?? makes a lot though.

    看前面的例子,用逻辑OR链接没有意义,用??代替。 虽然很多。

    let b = 0 ?? "bar" ?? "foo"; // 0

    The AND can’t be saved though.

    AND不能保存。

    我应该放弃合并吗? (Should I nullish coalesce?)

    I personally shall, if the project permits, but only when doing assignment checks as the expressions are much more pleasing to the eye than the regular null or undefiend checks and faster to mentally process.

    如果项目允许,我个人将这样做,但是只有在进行分配检查时,因为这些表达式比常规的null undefiend或无undefiend检查更令人赏心悦目,并且可以更快地进行心理处理。

    Chaining together with other logical operators does make a mental strain as there is no longer just truthy and falsly checks involved, but additional null/undefined ones which then get converted to truthy/falsy — they should be avoided to keep the code clean and coherent.

    与其他逻辑运算符一起链确实让精神紧张,因为不再只是truthy和falsly参与检查,但额外的null/undefined的人,然后被转换到truthy/falsy -他们应该避免保持代码的干净和连贯。

    普通英语JavaScript (JavaScript In Plain English)

    Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!

    喜欢这篇文章吗? 如果是这样,请订阅我们的YouTube频道解码,以获得更多类似的内容!

    翻译自: https://medium.com/javascript-in-plain-english/the-operator-you-are-probably-not-using-1105a3010c8a

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.011, SQL: 8