JavaScript

167 阅读1分钟

javaScript

大厂面试题总结

深拷贝

const deepCopy = (source) => {
    if(!isObject(source)) return source;
    let target = Array.isAarry(source) ? []: {};
    for(var k in source){
        if(source.hasOwnProperty(k)){
            if(typeof source[k] === 'object'){
                target[k] = deepCopy(source[k])
            } else {
                target[k] = source[k];
            }
        }
    }
    return target;
}`javascript
const isObject = (obj) => {
    return typeof obj === 'object' && obj !== null;
}

防抖节流

// 节流
const jieliu = (fn,wait=100) => {
    let num = 0;
    return (...arg) => {
        const _time = new Date().getTime()
        if (num - _tiem > wait) {
            fn.applay(this, arg)
            num = _itme
        }
    }
}
// 防抖
const fangdou =(fn, wait=100) => {
   let timer = 0;
   return (...args) => {
       if (timer) {
            clearTImeOut(timer)
        }
        timer = setTimeOut(() => {
            fn.apply(this,args)
        }, wait)
   }
}

构造函数

new Foo()  // 实例化对象 _proto_ 指向
Foo.prototype // 指向 Foo 的原型对象

eventLoop 事件轮循

Event Loop 是一个很重要的概念,指的是计算机系统的一种运行机制,I/O阻塞。每当遇到I/O的时候,主线程就让Event Loop线程去通知相应的I/O程序,然后接着往后运行,所以不存在红色的等待时间。等到I/O程序完成操作,Event Loop线程再把结果返回主线程。主线程就调用事先设定的回调函数,完成整个任务。

订阅发布模式

class Event{
    constructor() {
        this.callback={} //存储事件
    }
    on(type, fn){
        if(!this.callback[type]){
            this.callback[type]=[]
        }
        this.callback[type].push(fn);//将回调函数fn存入数组
        return this; // 返回this是为了实现链式调用
    }
    emit(type, ...params){
        this.callback[type].forEach(fn => fn(...params));
        return this;
    }
    once(type, fn){
        if(!this.callback[type]){
            this.callback[type] = [];
        }
        let _this = this; // 保存执行环境
        // 由于只能执行一次,这里需要做处理
        this.callback[type].push(function once(...args){
            fn(...args);
            _this.del(type,once);
        })
        return this;
    }
    del(type, fn){
        // 利用filter删除组组中
        this.callback[type] = this.callback[type].filter(cb => fn !== cb);
        return this;
    }
}

总结

后期正在逐步更新。。。。