//手写深拷贝
function cloneDeep(target){
let copy = target.constructor === Array ? [] :{};
for(let key in target){
if(target.hasOwnProperty(key)){
if(target[key]&& typeof target[key] === 'object'){
copy[key] =cloneDeep(target[key])
}else{
copy[key] = target[key]
}
}
}
return copy
}
解决json.parse && json.stringfy 深拷贝 关键字丢失问题
防抖
function showInfo(text){
console.log(this);
}
function debounce(func, wait) {
let timeout;
return function() {
// let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(this, args);
}, wait);
}
}
document.getElementById('input').addEventListener('input',debounce(showInfo,1000))
节流
function throttle(func,data) {
let timer = null;
let args = arguments;
return function() {
if (!timer) {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
timer = null;
}, data);
}
}
}
// document.getElementById('input').addEventListener('input',debounce(showInfo,1000))
document.getElementById('input').addEventListener('input',throttle(showInfo,500))