Skip to main content

经典面试题手写

function helloWorld() {
console.log('Hello, world!');
}

new_

原型链 + this指向

function new_(func, ...params) {
const obj = Object.create(func.prototype);
const result = func.apply(obj, params);
return typeof result === "object" ? result : obj;
}

防抖节流

防抖:截停动作频繁触发

节流:降低动作频次

function throttle(func, timeout) {
let timer = null
return function(...props) {
if(timer) {
return
}
timer = setTimeout(() => {
func(...props);
clearTimeout(timer)
timer = null
}, timeout)
}
}
function debounce(func, timeout) {
let timer = null
return () => {
if(timer){
clearTimeout(timer);
}

timer = setTimeout(() => {
func(...props);
clearTimeout(timer)
timer = null
}, timeout)
}
}