// 节流
let throttle = function(func, delay) {
let timer = null;
return ()=> {
if (!timer) {
timer = setTimeout(() => {
func.apply(this, arguments);
timer = null;
}, delay);
}
};
};
function handle() {
console.log(new Date());
}
window.addEventListener("scroll", throttle(handle, 1000));
// 防抖
function debounce(fn, wait) {
var timeout = null;
return () => {
if (timeout !== null) clearTimeout(timeout);
timeout = setTimeout(()=> {
fn.apply(this, arguments);
timeout = null;
}, wait);
};
}
function handle() {
console.log(new Date());
}
window.addEventListener("scroll", debounce(handle, 1000));
// proxy
let handler = {
get(target, key, receiver) {
console.log("get", key);
return Reflect.get(target, key, receiver);
},
set(target, key, value, receiver) {
console.log("set", key, value);
return Reflect.set(target, key, value, receiver);
}
};
let obj = {name: '张三', age: 23};
let proxy = new Proxy(obj, handler);
proxy.name = "李四";
proxy.age = 24;