常见函数手写
防抖(debounce)
防止用户频繁触发,只会在最后一次触发后执行
节流(throttle)
防止用户频繁触发,无论何时触发,只会一定时间执行一次
新建对象实例(new)
手写 new 函数
随机值(random)
获取 min 到 max 范围内的最小值
核心代码
/**
 * 防抖
 */
export const debounce = (time: number) => {
    let timeout: NodeJS.Timeout;
    return (fn: (...args: any[]) => void) => {
        if (timeout) clearTimeout(timeout);
        timeout = setTimeout(fn, time) as any;
    };
};
/**
 * 节流
 */
export const throttle = (time: number) => {
    let run = false;
    return (fn: (...args: any[]) => void) => {
        if (run) return;
        run = true;
        setTimeout(() => {
            fn();
            run = false;
        }, time);
    };
};
/**
 * 新建对象实例
 */
export const newInstance = (fn: (...args: any[]) => any, ...args: unknown[]) => {
    const instance = Object.create(fn.prototype);
    const res = fn.apply(instance, args);
    return res instanceof Object ? res : instance;
};
/**
 * 随机值[a,b]
 * @param min 最小值
 * @param max 最大值
 */
export function random(min: number, max: number) {
    return min + Math.floor(Math.random() * (max - min + 1));
}