常用函数
https://juejin.im/post/6873513007037546510
一、防抖
function debounce(func,ms = 500){
let timer
return function(...arges) {
if(timer) {
clearTimeout(timer)
}
timer = setTimeout( () => {
func.apply(this,args)
},ms)
}
}
二、节流
function throttle(func,ms) {
let canRun = true
return function(...args) {
if(!canRun) return
canRun = false;
setTimeout( () => {
func.apply(this,args)
canRun = true
},ms)
}
}
三、new
function myNew(Func) {
const instance = {}
if(Func.prototype){
Object.setPrototypeOf(instance,Func.prototype)
}
const res = Func.apply(instance,[].slice.call(arguments,1))
if(typeof res === 'function' || (typeof res === "object" && res !== null)){
return res
}
return instance
}
四、bind
待添加
876

被折叠的 条评论
为什么被折叠?



