1: 沉睡排序
1 var numbers=[1,2,3,4,5,5,99,4,20,11,200]; 2 numbers.forEach((num)=>{ 3 setTimeout(()=>{ 4 console.log(num) 5 },num) 6 })
2: 快速去重 (ES6)
1 var arr = Array.from(new Set([1,2,3,4,4,3,5,6,7,8,8]));
3、单行写一个评级组件
1 "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
定义一个变量rate是1到5的值,然后执行上面代码,看图
才发现插件什么的都弱爆了
4、论如何优雅的取整
var a = ~~2.33 var b= 2.33 | 0 var c= 2.33 >> 0
5、短路表达式
条件判断
1 var a = b && 1 2 // 相当于 3 if (b) { 4 a = 1 5 } else { 6 a = b 7 } 8 9 var a = b || 1 10 // 相当于 11 if (b) { 12 a = b 13 } else { 14 a = 1 15 }