- 深度克隆
function deepClone(source) { // 判断是否数组或对象 if (source.constructor != Array && source.constructor != Object) { return source } const targetObj = source.constructor === Array ? [] : {} for(let key in source) { // 判断是否为自身属性 if (source.hasOwnProperty(key)) { if (source[key] && typeof source[key] === 'object') { // 数组或对象 targetObj[key] = deepClone(source[key]) } else { // 基本类型 targetObj[key] = source[key] } } } return targetObj }
- 节流
function throttle(func, delay) { let lastCall = 0 return function (...args) { const now = new Date().getTime() if (now - lastCall < delay) { return; } lastCall = now return func.apply(this, args) } }
- 防抖
function debounce(func, delay) { let timeout return function (...args) { clearTimeout(timeout) timeout = setTimeout(() => { func.apply(this, args) }, delay) } }