手写数组去重
1、let arr = [1,2,3,3,4,4,5,5]
function cut(arr){
let newList = []
for(let i = 0
if(newList.indexOf(arr[i]) == -1){
newList.push(arr[i])
}
}
return newList
}
let List = cut(arr)
2、let arr = [1,2,3,3,4,4,5,5]
function cut(arr){
for(let i = 0
for(let j = 0
if(arr[i] == arr[j] && i != j){
arr.splice(j,1)
}
}
}
return arr
}
let List = cut(arr)
手写深拷贝
1、 let obj = {
a:{aaa:[{a:'1'},{b:'2'},{c:3}]},
rrr:'123',
ttt:undefined,
yyy:null
}
function clone(obj){
let newObj = {}
for(let key in obj){
newObj[key] = obj[key]
}
return newObj
}
let newList = clone(obj)
2、
let newList = JSON.parse(JSON.stringfly(obj))
手写发布订阅
1、
let eventBus = {
arrays:{},
on:function(type,fn){
this.arrays[type] = this.arrays[type] || []
this.arrays[type].push(fn)
},
emit:function(type,data){
if(!this.arrays[type])return
this.arrays[type].map((fn)=>{
fn(data)
})
}
}
eventBus.on('click',function(data){
console.log('消息订阅1:'+data);
})
eventBus.emit('click','发布啦')
手写Ajax
手写防抖(throttle) 和 节流(debounce)
1、
function throttle(){
if(timer){
clearTimeout(timer);
timer = setTimeout(function(){
number ++
let Hthrottle = document.getElementsByClassName('Hthrottle')
console.log(Hthrottle);
Hthrottle.innerHTML= '防抖:'+number;
},1000)
}else{
timer = setTimeout(function(){
number ++
let Hthrottle = document.getElementsByClassName('Hthrottle')
console.log(Hthrottle);
Hthrottle.innerHTML= '防抖:'+number;
},1000)
}
}
2、
function debounce(){
if(timer){
return
}else{
timer = setTimeout(function(){
let Hdebounce = document.getElementsByClassName('Hdebounce')
console.log(number);
number ++
Hdebounce.innerHmtl = '节流:'+number
timer = null
},1000)
}
}
手写promise.all
手写简单版promise