1. Copy n characters
1 function (num) {2 return (new Array (++num)). Join (this); 3 }4 5var a = ' a '; 6 a.repeat (5); // ' AAAAA '
2. Replace If...else ...
1 var result; 2 3 result = IsTrue? something:anotherthing; 4 result = Something | | anotherthing; 5 result = something && anotherthing;
3. Multi-Conditional judgment
Maybe:
1 functionYourfun (status) {2 varcolor;3 Switch(status) {4 Case0:5color = ' White ';6 Break;7 Case1:8color = ' Red ';9 Break;Ten Case2: Onecolor = ' Yellow '; A Break; - Case3: -color = ' green '; the Break; - Case4: -color = ' Blue '; - Break; + } - + returncolor; A}
Better:
1 function Yourfun (status) {2 return [' White ', ' red ', ' yellow ', ' green ', ' Blue '][status] 3 }
. Thinking
Ensure code readability, flexible application of JS code skills. On the one hand to improve their coding ability, on the one hand to deepen the understanding of JS.