[The following is an email sent to a colleague yesterday. To prevent leaks of trade secrets, the project name and variable name are hidden.]
========================================================== ================
Yesterday we found a bug in Nx code, which was caused by the JavaScript operator priority.
Example:
If (! A instanceof Array)
Throw new Error ("a shocould be an array ");
......
If (! B instanceof Object)
Throw new Error ("B shocould be an object ");
Cause :! Operator has a higher priority than instanceof, So calculate it first! A. The result is of the boolean type.
While
True instanceof Array;
False instanceof Array;
True instanceof Object;
False instanceof Object;
Returns false, which causes all checks to fail.
The following is the operator priority in JavaScript for your reference.
Operator priority (JavaScript)
4 (in total, 5) This article is helpful-evaluate this topic
The operator priority describes the sequence in which operations are performed during expressions. First, the operation with a higher priority is executed, and then the operation with a lower priority is executed. For example, multiply and then add.
JavaScript Operators
The following table lists the JavaScript operators in priority order. Operators with the same priority are calculated from left to right.
Operator |
Description |
. [] () |
Field access, array indexing, function calling, and expression grouping |
++ ---~ ! Delete new typeof void |
Unary operator, returned data type, object creation, undefined Value |
*/% |
Multiply, divide, and calculate the remainder |
+-+ |
Addition, subtraction, String concatenation |
<>>>> |
Shift |
<<=>> = Instanceof |
Is an instance of a specific class Less Than, less than or equal to, greater than, greater than or equal to, or greater than or equal? |
=! ===! = |
Equal, unequal, full, incomplete, etc. |
& |
Bitwise "and" |
^ |
Bitwise "XOR" |
| |
Bitwise "or" |
&& |
Logic and |
| |
Logic "or" |
? : |
Conditional operation |
=OP= |
Assignment and assignment operations (for example, + = and & =) |
, |
Multiple computing |
Parentheses are used to change the computational order determined by the operator priority. This means that the expression in parentheses is calculated first, and then its value is used for the rest of the expression.
Source: http://technet.microsoft.com/zh-cn/library/z3ks45k7