此检查如何与 JS 中的运算符优先级一起使用
How does this check work with operator precedence in JS
所以我正在阅读这个优先顺序 table https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
它表示 20 - 1,其中 20 是最高优先级。
16 Logical NOT right-to-left ! …
所以 !
运算符的优先级为 16.
10 Strict Equality … === …
因此 ===
运算符的优先级为 10。
这行
!'hello' === 'goodbye'
这是如何得到executed/read的?通过阅读它,我想。逐步进行;
'hello' === 'goodbye' 然后检查,反转布尔值。因此,如果它 returns true 将其设置为 false。
如果我正在通读优先运算符 table。在我看来,它首先执行 !
运算符,然后执行 ===
.
它如何预先反转一个非布尔值然后进行真实性检查?有人可以解释一下它是如何工作的吗?
谢谢!
It looks to me like it does the ! operator first and then ===.
是的。 16是比10大的数,所以!
的优先级比===
高,所以优先解决。
How does it invert a non-bool value beforehand and then do the truthy check?
请参阅 the spec for ! which points to ToBoolean 内容:
String: Return false if argument is the empty String (its length is zero); otherwise return true.
所以我正在阅读这个优先顺序 table https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
它表示 20 - 1,其中 20 是最高优先级。
16 Logical NOT right-to-left ! …
所以 !
运算符的优先级为 16.
10 Strict Equality … === …
因此 ===
运算符的优先级为 10。
这行
!'hello' === 'goodbye'
这是如何得到executed/read的?通过阅读它,我想。逐步进行;
'hello' === 'goodbye' 然后检查,反转布尔值。因此,如果它 returns true 将其设置为 false。
如果我正在通读优先运算符 table。在我看来,它首先执行 !
运算符,然后执行 ===
.
它如何预先反转一个非布尔值然后进行真实性检查?有人可以解释一下它是如何工作的吗?
谢谢!
It looks to me like it does the ! operator first and then ===.
是的。 16是比10大的数,所以!
的优先级比===
高,所以优先解决。
How does it invert a non-bool value beforehand and then do the truthy check?
请参阅 the spec for ! which points to ToBoolean 内容:
String: Return false if argument is the empty String (its length is zero); otherwise return true.