简体   繁体   English

此检查如何与JS中的运算符优先级一起使用

[英]How does this check work with operator precedence in JS

So I'm reading through this precedence table https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence 因此,我正在阅读此优先级表https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

It says 20 - 1 with 20 being highest precedence. 它说20-1,其中20为最高优先级。

16 Logical NOT right-to-left ! … 16 Logical NOT right-to-left ! … So the ! 16 Logical NOT right-to-left ! …那么! operator has a precedence of 16. 运算符的优先级为16。

10 Strict Equality … === … So the === operator has a precendence of 10. 10 Strict Equality … === …因此===运算符的优先级为10。

This lines 这条线

!'hello' === 'goodbye'

How does this get executed/read? 如何执行/读取? By reading it I thought. 通过阅读,我想。 In steps it goes; 分步进行;

'hello' === 'goodbye' Check then, invert bool value . 'hello'==='再见'然后检查, 反转bool值 So If it returns true set it to false. 因此,如果返回true,则将其设置为false。

If i'm reading through the precedence operators table. 如果我正在阅读优先运算符表。 It looks to me like it does the ! 在我看来,确实如此! operator first and then === . 运算符,然后===

How does it invert a non-bool value beforehand and then do the truthy check? 它如何预先转换非布尔值,然后进行真实性检查? Like how does it work could someone explain? 像有人可以解释它是如何工作的?

Thank you! 谢谢!

It looks to me like it does the ! 在我看来,确实如此! operator first and then ===. 运算符,然后===。

Yes. 是。 16 is a higher number than 10, so ! 16比10高,所以! has a higher precedence than === , so it is resolved first. 具有比===更高的优先级,因此先解决。

How does it invert a non-bool value beforehand and then do the truthy check? 它如何预先转换非布尔值,然后进行真实性检查?

See the spec for ! 请参阅规格! which points to ToBoolean which says: 指向ToBoolean表示:

String: Return false if argument is the empty String (its length is zero); 字符串:如果参数为空字符串(其长度为零),则返回false;否则,返回false。 otherwise return true. 否则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM