This article mainly introduces the alternative usage of Javascript & amp; and |. If you need it, you can refer to it and have no idea about writing articles recently, I feel that there are always endless tasks. However, I started to study Titanium over the past few days and found that its official MVC Framework (Alloy) is still quite good. At the beginning, I learned little about the code, so I haven't studied it in detail. Later I found that the official CodeStrong is a set of very good learning code. As long as I read the entire set of code, I believe the use of Alloy will basically be ~
While looking at its source code, it is found that many places use the following usage:
$.clouds && ($.index.add($.clouds));
I didn't quite understand it at the beginning. After all, I usually used a little bit of it. After I got to google, I realized that this method was very convenient and easy to use (in fact, this method is also widely used in jquery's source code ). The following is a reference to the online explanation & | an alternative usage in javascript:
A () & B (): If true is returned after a () is executed, B () is executed and the value of B is returned. If false is returned after a () is executed, then the entire expression returns the value of a (), and B () is not executed;
A () | B (): If true is returned after a () is executed, the entire expression returns the value of a (). B () is not executed. If a () is executed () if false is returned, B () is executed and the value of B () is returned;
& The priority is higher than |
It is quite clear after reading it. Let's look at the specific code:
Alert (1 & 3 | 0) & 4); // Result 4 ① alert (1 & 3 | 0 & 4 ); // result 3 ② alert (0 & 3 | 1 & 4); // Result 4 ③
Analysis:
Statement ①: 1 & 3 returns 3 => 3 | 0 returns 3 => 3 & 4 returns 4
Statement ②: Execute 1 & 3 first, return 3, return 0 in execution 0 & 4, and finally compare 3 | 0 returns 3
Statement ③: Execute 0 & 3 first, return 0, return 4 in execution 1 & 4, and finally compare 0 | 4 returns 4
Note: All integers other than 0 are true, and "undefined, null, and null" is false.
I think javascript is really flexible ~~