First, let's get a question:
The following describes the rules for displaying the growth rate:
1 arrow is displayed when the growth rate is 5;
The growth rate is 10 and two arrows are displayed;
3 arrows are displayed at a speed of 12;
4 arrows are displayed at a rate of 15;
Each other shows 0 arrows.
UseCodeHow to implement it?
If, else:
JS Code
VaR add_level = 0; <br/> If (add_step = 5) {<br/> add_level = 1; <br/>}< br/> else if (add_step = 10) {<br/> add_level = 2; <br/>}< br/> else if (add_step = 12) {<br/> add_level = 3; <br/>}< br/> else if (add_step = 15) {<br/> add_level = 4; <br/>}< br/> else {<br/> add_level = 0; <br/>}
Better switches:
JS Code
VaR add_level = 0; <br/> switch (add_step) {<br/> case 5: add_level = 1; <br/> break; <br/> case 10: add_level = 2; <br/> break; <br/> case 12: add_level = 3; <br/> break; <br/> case 15: add_level = 4; <br/> break; <br/> default: add_level = 0; <br/> break;
If the requirement is changed:
4 arrows are displayed when the growth rate is greater than 12;
3 arrows are displayed when the growth rate is greater than 10;
2 arrows are displayed when the growth rate is greater than 5;
1 arrow is displayed when the growth speed is greater than 0;
The growth speed is <= 0. 0 arrows are displayed.
So it is also very troublesome to implement with the switch.
Have you ever thought about implementing it with a single line of code?
OK. Let's take a look at the strong expressiveness of JS:
JS Code
VaR add_level = (add_step = 5 & 1) | (add_step = 10 & 2) | (add_step = 12 & 3) | (add_step = 15 & 4) | 0;
More powerful and better:
JS Code
VaR add_level = {'5': 1, '10': 2, '12': 3, '15': 4} [add_step] | 0;
Second requirement:
JS Code
VaR add_level = (add_step> 12 & 4) | (add_step> 10 & 3) | (add_step> 5 & 2) | (add_step> 0 & 1) | 0;
First, let's sort out the next concept. Remember: In JS logic operations, 0, "", null, false, undefined, and Nan are all judged as false, all others are true (it seems that nothing is missing, please confirm ). Remember this, otherwise the application | and & will have problems.
By the way, I am often asked if (!! ATTR), why not directly write if (ATTR );
In fact, this is a more rigorous writing:
Test typeof 5 and typeof !! 5 .!! Is to convert a variable of other types to the bool type.
The following describes the logical operators & |.
In almost all languages, | and & follow the "Short Circuit" principle. For example, if the first expression in & is false, it will not process the second expression, but | is the opposite.
JS also follows the above principles. But it is interesting that they return values.
Code: var ATTR = true & 4 & "AAA ";
The running result of ATTR is not simply true or false, but "AAA"
Let's take a look. |:
Code: var ATTR = ATTR | ""; this operation is often used to determine whether a variable has been defined. If it is not defined, an initial value is given, this is useful when defining a default value for function parameters. Because JS does not support defining func ($ ATTR = 5) directly on type parameters like PHP ). Remind you again to remember the above principle: if the real parameter needs to be 0, "", null, false, undefined, Nan, it will also be treated as false.
If (A> = 5 ){
Alert ("hello ");
}
Can be written:
A> = 5 & alert ("hello ");
This requires only one line of code. Note that the features of | and & in JS help us streamline the code and reduce the readability of the Code. This requires us to weigh ourselves.
On the one hand, streamlining JS Code can substantially reduce network traffic, especially JS public libraries that are used in a large number of applications. I personally recommend that you write comments if the application is relatively complex. This is the same as the regular expression. It can simplify the code, but the readability is reduced, and the requirement on the person who reads the code is higher. The best way is to write comments.
We may not use these skills, but we must be able to understand them, because these skills have been widely used, especially the code in JS boxes such as jquery, if you don't understand this, you can hardly understand other people's code.
Such as VAR Yahoo = Yahoo | {}; is widely used.
OK. Let's take a look at the code in jquery:
JS Code
VaR wrap = <br/> // option or optgroup <br/>! Tags. indexof ("<Opt") & <br/> [1, "<select multiple = 'Multiple '> ", "</SELECT>"] | <br/>! Tags. indexof ("<leg") & <br/> [1, "<fieldset>", "</fieldset>"] | <br/> tags. match (/^ <(thead | tbody | tfoot | colg | cap)/) & <br/> [1, "<Table> ", "</table>"] | <br/>! Tags. indexof ("<TR") & <br/> [2, "<Table> <tbody> ", "</tbody> </table>"] | <br/> // <thead> matched above <br/> (! Tags. indexof ("<TD") |! Tags. indexof ("<TH") & <br/> [3, "<Table> <tbody> <tr> ", "</tr> </tbody> </table>"] | <br/>! Tags. indexof ("<col") & <br/> [2, "<Table> <tbody> </tbody> <colgroup> ", "</colgroup> </table>"] | <br/> // Ie can't serialize <link> and <SCRIPT> tags normally <br/>! Jquery.support.html serialize & <br/> [1, "Div <div>", "</div>"] | <br/> [0 ,"", ""]; <br/> // go to HTML and back, then peel off extra wrappers <br/> Div. innerhtml = wrap [1] + ELEM + wrap [2]; <br/> // move to the right depth <br/> while (WRAP [0] --) <br/> DIV = div. lastchild;
When this code is used by the author to process $ (HTML), some labels must be restricted. For example, <option> must be within <SELECT> </SELECT>.
You may also find that the author has another coincidence! Tags. indexof ("<Opt"), the author easily implemented the startwith function without any additional code. JquerySource codeThere are also a lot of exquisite code that you can learn.