Javascript & | alternative usage-javascript tips-js tutorial

Source: Internet
Author: User
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 ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.