Today I saw a piece of YUI compressor compressed js code:
userNum && (ind += index,ind >= userNum && (ind -= userNum),ind < 0 && (ind === -2 && (ind = -1),ind += userNum),selLi.removeClass("on"),$(selLi[ind]).addClass("on"));
I went crazy, and probably few people could understand it all at once. Then "translate" him.
&& (Logistic and)
Here is mainly a "&&" operation. First of all, you need to understand this and see a simple example:
1 var a = 1 && 2 && 3;//3
2 var b = 0 && 1 && 2;//0
3 var c = 1 && 0 && 2;//0
4 alert(a), alert(b), alert(c);
Hehe, the writing method is very strange, the result of running is 3, 0, 0. Generally we often use it in if statements. The "&&" (logical and) operation is really the opposite of "||" operation. The "&&" operation returns when it encounters false.
For example: a && b , if a is true, directly return b, regardless of whether b is true or false. If a is false, then return a directly. In the example above, the first var a = 1 && 2 && 3;Because 1 && 2, 1 is true, return 2; 2&&3, 2 is true, return 3.
After understanding the "&&" operation, then look at the YUI compressor compressed js code in the top surface, and translate it:
if(userNum){ind+=index;if(ind>=userNum){ ind-=userNum;} if(ind < 0){if(ind === -2){ind = -1;} ind += userNum;}selLi.removeClass("on");$(selLi[ind]).addClass("on"); }To be ashamed, I am old and have been "translating" for half an hour, but it is only with the help of my colleagues that the "translation" is correct.
||(logical or)
Let’s take a look at the “||” (logical or) operation, and see the example:
1 var a = 0 || 1 || 2;//1
2 var b = 1 || 0 || 3;//1
3 alert(a), alert(b);
The "||" operation returns when it encounters true. For example: a || b , if a is false, directly return b, regardless of whether b is true or false. If a is true, it will return directly and will not continue to execute.
&& (logical and) and || (logical or) should be paid attention to their priority:
&& (Logic &) Priority is higher than || (Logic or)
return a && b || c ,
We can judge the return value based on a. If a is false, it will definitely return c; if b and c are both true, then we can decide whether b or c based on a. If a is false, it will return c. If a is true, it will return b.
return a || b && c
According to the priority, b && c is calculated first, and then a is or with a; if a is true, it returns a, whether it is b or c, if a is false, then if b is false, it returns b, and if b is true, it returns c;
1 var a = 3 && 0 || 2; //2
3 var b = 3 || 0 && 2; // 3
5 var c= 0 || 2 && 3; // 3
6 alert(a), alert(b), alert(c);
Also attached: JS operator priority (listed from high to low)
| Operators | describe |
| . [] () | Field access, array subscript, function calls, and expression grouping |
| ++ -- - ~ ! delete new typeof void | Unary operator, return data type, object creation, undefined value |
| */ % | Multiplication, division, mold extraction |
| + - + | Addition, subtraction, string concatenation |
| << >> >>>> | Shift |
| < <= > >= instanceof | less than, less than or equal to, greater than, greater than or equal to, instanceof |
| == != === !== | Equal, not equal, strictly equal, not strictly equal |
| & | bitwise and |
| ^ | bitwise xor |
| | | bitwise or |
| && | Logic and |
| || | Logical or |
| ?: | condition |
| = oP= | Assignment, operational assignment |
| , | Multiple evaluation |
The above article briefly discusses JS operators && and || and their priorities are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.