经典面试题手写
- JavaScript
- Python
function helloWorld() {
console.log('Hello, world!');
}
def hello_world():
print("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)
}
}