對于奇舞團(tuán)的成員,月影要求我們的代碼要優(yōu)雅一點(diǎn)、更優(yōu)雅一點(diǎn),最好是令人驚嘆的??
數(shù)組去重
寫法零, ES6 Set + filter,常用思路
arrayUnique : (arr) => {
let s = new Set();
return arr.filter(function(o){
if(s.has(o)){
return false
}
else{
s.add(o);
return true
}
});
}
寫法一,最"low"版,indexOf
function arrayUnique(arr){
const ret = []
for(let i = 0; i < arr.length; i++){
const item = arr[i],
idx = ret.indexOf(item)
if(idx < 0)
ret.push(item)
}
return ret
}
const arr = [1,3,1,2,2,4,3,4,1]
console.log(arrayUnique(arr))
寫法二,利用Object的key的唯一性
function arrayUnique(arr){
const map = {}, ret = []
for(let i = 0; i < arr.length; i++){
const item = arr[i]
if(map[item] == null){
map[item] = i
ret.push(item)
}
}
return ret
}
const arr = [1,3,1,2,2,4,3,4,1]
console.log(arrayUnique(arr))
寫法三,利用filter + index
function arrayUnique(arr){
return arr.filter((item,i) => arr.indexOf(item) === i)
}
const arr = [1,3,1,2,2,4,3,4,1]
console.log(arrayUnique(arr))
寫法四,純Set,最史上最簡單的數(shù)組去重
function arrayUnique(arr){
return [...new Set(arr)]
}
const arr = [1,3,1,2,2,4,3,4,1]
console.log(arrayUnique(arr))
月大大感言:我們要熟悉API
for in 神坑
猜下下面代碼輸出什么:
var scores = [10,11,12]
var total = 0
for(var score in scores){
total += score
}
var mean = total/scores.length
console.log("mean:",mean)

竟然是4!
當(dāng)然了,把每一步console出來就知道為什么了。

score是一個字符串,每一步total都是作為字符串去拼接,但最后又變成了number去相除。
另外,for in 會把自定義在prototype上的屬性打印出來,所以用for in的時候要在 defineProperty的時候enumerable設(shè)置為false,或是用hasOwnProperty判斷一下;或者,用for of:
Array.prototype.contains = function(item){
return this.indexOf(item) >= 0
}
const arr = [1,2,3,4]
for(let key in arr){
console.log(key)
}
for(let [key,value] of Object.entries(arr)){
console.log(key)
}

異步的改良
交通燈。
頭大的寫法:
function main(){
setTimeout(() => {
traffic.className = 'pass'
setTimeout(() => {
traffic.className = 'wait'
setTimeout(() => {
traffic.className = 'stop'
main()
},1500)
},3000)
},3000)
}
main()
然后我們有了promise:
function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
function main(){
traffic.className = 'stop'
wait(3000).then(() => {
traffic.className = 'pass'
return wait(3000)
}).then(() => {
traffic.className = 'wait'
return wait(1500)
}).then(main)
}
main()
但是我們還有更牛X的async/await:
function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
async function main() {
//no protect
while(true){
traffic.className = 'stop'
await wait(3000)
traffic.className = 'pass'
await wait(3000)
traffic.className = 'wait'
await wait(1500)
}
}
main()
創(chuàng)建數(shù)組,生成0~51
小萌新寫法
function generateCards(n){
const ret = []
for(let i=0; i<n; i++){
ret.push(i)
}
return ret
}
let cards = generateCards(52)
console.log(cards)
老油條寫法
function generateCards(n){
return Array.from({length:n}).map((_, i) => i)
}
let cards = generateCards(52)
console.log(cards)
為什么不能直接用Array.from(52)?

只new一個52的array,是empty的,無法用map遍歷。但是用
Array.from({length:52}),生成的是52個empty,可以遍歷出來:
但是顯然還有更好的辦法,也就是黑山老妖們寫出來的:
function *$cards(n = 52){
for(let i = 0; i < n; i++)
yield i
}
let cards = [...$cards]
console.log(cards)
Array構(gòu)造函數(shù)的二義性
const a = new Array(10),
b = new Array(10,9),
c = new Array(10,9,8)
console.log(a,b,c)

可以用Array.of
const a = new Array(10),
b = new Array(10, 9),
c = new Array(10, 9, 8)
console.log(a, b, c)
const d = Array.of(10),
e = Array.of(10, 9),
f = Array.of(10, 9, 8)
console.log(d, e, f)

空/稀疏數(shù)組的遍歷
Array.from、fill
未完,待續(xù)。。。