<body>
<button>防抖</button>
<script>
const array = [
{ label: '1', value: 'a' },
{ label: '2', value: 'b' },
]
const method = (arr) => {
let obj = {}
arr.forEach((item) => {
obj[item.value] = item.label
})
return obj
}
console.log(JSON.stringify(method(array)))
function bind(fn, context) {
return function () {
return fn.apply(context, arguments)
}
}
function fn() {
console.log(this)
}
const bindFn = bind(fn, 'hello')
bindFn()
let haha = debounce(fn, 2000)
document.querySelector('button').addEventListener('click', haha)
function debounce(fn, delay) {
let timer = null
return function () {
let _this = this
let args = arguments
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(_this, args)
}, delay)
}
}
</script>
</body>