'use strict'
function debounce(fn, delay = 500) {
let timer = null
return (...args) => { //箭头函数
clearTimeout(timer)
timer = setTimeout(() => {
console.log(this) //使用严格模式:undefined 不使用严格模式:Window
fn.apply(this, args)
//这里的 this 是 undefined 为什么后续的代码还能正常的运行呢?
}, delay)
}
}
const input1 = document.getElementById('input1')
input1.addEventListener(
'keyup',
debounce((ev) => {
console.log(ev.target) // <input type="text" id="input1">
console.log(ev.target.value)
}, 600)
)