关于 this 的问题
来源:16-8 手写防抖 debounce

h4ck3r
2020-06-22
'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)
)
写回答
1回答
-
双越
2020-06-22
这里的 this 是 undefined 为什么后续的代码还能正常的运行呢? —— 因为后续的代码没有用到 this 。
022020-06-22
相似问题