Today, when the plug-in boxscroll, because if the conditions inside the judge more than two, so immediately want to rewrite switch. Change to half, suddenly remember Jshint and other code quality testing tool in a request, with = = = Replace = =, without unreliable forced transformation. Then suddenly guess, change to switch will not reduce efficiency ah? Switch inside the actual judgment is = = = = = =?
With the idea, hurriedly raise a chestnut, good eat:
var a = ' 5 ';
Switch (a) {case
5:
console.log (' = = ');
break;
Case "5":
console.log (' = = = ');
break;
Default:
}
Finally, the console shows the = = =, it seems to be safe to use. Look for the previous study notes, well, it does say that the switch judgment is the strict equality operator, so no type conversions occur. Here's a summary.
1.if and switch
If is the most used, there is not much to say. It is worth noting that if actually and | | Quite like, if the Conditiona in if (Conditiona) {} else {} is true, then the code block before it executes else does not look at the code in the else. and | | The previous one will be ignored when true, even if there are many errors in it. Based on this nature, it is of course possible to put the most likely blocks of code in front, reducing the number of judgments. On the other hand, if there are a lot of if judgments, and the number of possible execution of the distribution is more uniform, then the subsequent judgment statement each time to the previous judgment one after execution, not conducive to optimization. A better approach is to turn a judgment statement into a two-level judgment statement, such as
if (a > 0 && a <= 1) {
//do something
} else if (a > 1 && a <= 2) {
} else if (A & Gt 2 && a <= 3) {
} else if (a > 3 && a <= 4) {
} else if (a > 4 && a <= 5) c6/>} else if (a > 5 && a <= 6) {
} ...
Into
if (a > 0 && a <= 4) {
if (a <= 1) {
//do something
} else if (a > 1 && a <= 2) {
} else if (a > 2 && a <= 3) {
} else if (a > 3 && a <= 4) {
}
} else if (a > 4 && a <= 8) {
//
}.
Although each of the previous judgments were added once, the subsequent judgements were reduced (4-1) *n times, or full profit. Suddenly feel this way and nested loops a bit like, the number of cycles on the outside to help optimize performance, how to split into two or even multiple layers depends on the specific situation.
Switch is if the most intimate comrades, every time if busy not come over to take a hand. Switch and if interchange estimates are nothing to talk about, and switch and if, like, are ordered from the top down, but the difference is that if else in the switch doesn't work, it has its own little brother: break. If you do not encounter Break,switch will continue to execute, as
var a = 2;
Switch (a) {case
1:
console.log ("1");
Break Miss Case
2:
console.log ("2");
Case 3:
console.log ("3");
Default:
console.log (' no Break ');
}
The last console displays the 2,3,no break. In fact, well understand, break is prompted to jump out of the internal execution body to the next case to judge, if not, the equivalent of if (condition) {a}{b}, without Else,a and B of course will be carried out. There are two small tip, one is switch and case can write any expression, such as
Switch (A + b) {case
A * b:
console.log ("1");
break;
Case A/b + C: Break
;
//...
Default:
console.log (' no Break ');
}
The actual comparison is (a+b) = = = (a*b) and (a+b) = = = (a/b+c). Second, there is a special use of switch, such as
Switch (true) {case
condition1:
//do something
;
Case Condition2: Break
;
//...
Default:
///..
;
}
Each case in the switch is then judged to be executed in order. As for switch (false), there is no egg to use.
2.== and = =
In a word, the congruent and unequal operators are no different from the equality and inequality operators, except that the operands are not converted before the comparison is done.
The most classic case
var a = "5",
B = 5;
A = = b//true a = = = B //false
var a = "ABC",
B = "AB" + "C";
A = = = B //true
The following reason for true is actually inseparable from the type of string that cannot be changed. The surface looks like B is simply stitching a string, but in fact it has nothing to do with the original B. Each string has a specific place in the memory pool, and when b= "AB" + "C" is executed, the strings AB and C are destroyed, while B points to the location of the ABC in the Memory pool. Because the string ABC was found in the memory pool before pointing (because it is referenced by a), B points to the same area as a, and the congruent judgment is equal. If there is no variable before b that points to the string ABC, then there is no in the memory pool where it is set to the ABC, and b is pointed to ABC.
Attached to the previous summary figure two:
The above mentioned is the entire content of this article, I hope you can enjoy.