#### 1. 多種方式實現數組去重、扁平化、對比優缺點
1. 扁平化:兩種方法及更多
```
let arr = \[1,2,\[3,4\],\[5,6,\[7,\[8,9,\[10\]\]\]\]\]
// 方法一:使用遞歸扁平化
function flat(arr) {
let res = \[\]
arr.forEach(item \=> {
if(Array.isArray(item)) {
res = res.concat(flat(item))
} else {
res.push(item)
}
})
return res
}
console.log( flat(arr) )
//方法二,如果數組元素都是數字則使用toString()方法 //缺點:受限于數組內的元素只能為Number類型,場景非常有限
function flat2(arr) {
return arr.toString().split(',').map(item \=> {
return +item
})
}
console.log( flat2(arr) )
```
#### 2. 多種方式實現深拷貝、對比優缺點
#### 3. 手寫函數柯里化工具函數、并理解其應用場景和優勢
#### 4. 手寫防抖和節流工具函數、并理解其內部原理和應用場景
1.防抖:debounce:事件在N秒內只執行一次,如果在N秒內又觸發了事件,則時間重新開始計算
```
function debounce(fn, delay) {
let first = true
let timer = null
return function(){
const _this = this
if (first) {
fn.call(_this, arguments)
return
}
clearTimeout( timer )
timer = setTimeout( function(){
fn.call(_this, arguments)
}, delay || 500)
}
}
```
2.節流函數:throttle:讓頻繁觸發的事件在指定的時間內只觸發一次,比如:scroll,mousemove事件等
```
function Throttle(fn, delay) {
let first = true;
let timer = null;
let _fn = fn
return function() {
const _this = this
if (first) {
_fn.call(_this, arguments)
first = false
return
}
if (timer) return
timer = setTimeout(() => {
clearTimeout(timer)
timer = null
_fn.call(_this, arguments)
}, delay || 1000)
}
}
```
#### 5. 實現一個sleep函數