1 String.prototype.repeat = function(num){ 2 return (new Array(++num)).join(this); 3 } 4 5 var a = ‘A‘; 6 a.repeat(5); //‘AAAAA‘
1 var result; 2 3 result = isTrue ? something : anotherthing; 4 result = something || anotherthing; 5 result = something && anotherthing;
Maybe:
1 function yourFun(status){ 2 var color; 3 switch(status){ 4 case 0: 5 color = ‘white‘; 6 break; 7 case 1: 8 color = ‘red‘; 9 break; 10 case 2: 11 color = ‘yellow‘; 12 break; 13 case 3: 14 color = ‘green‘; 15 break; 16 case 4: 17 color = ‘blue‘; 18 break; 19 } 20 21 return color; 22 }
Better:
1 function yourFun(status){ 2 return [‘white‘, ‘red‘, ‘yellow‘, ‘green‘, ‘blue‘][status] 3 }
保证代码可读性,灵活应用JS代码技巧。一方面提升自己的编码能力,一方面加深对JS的理解。
原文地址:http://www.cnblogs.com/zhouwenhong/p/3835988.html