手写instanceof

84 阅读1分钟

手写instanceof

什么是instanceof

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上 可以判断继承关系,只要是在同一条原型链上,就可以返回true

    function fun(obj1,obj2){
        let objs1 = obj1.__proto__
        let objs2 = obj2.prototype
        while(true){
            if(obj1 === null){
                return false
            }else if(obj1 === obj2)
                return true
        }
        obj1s = obj1s.__proto__
    }

手写bind函数

function fun(a,b,c){
            console.log(this)
            console.log(a,b,c)
            return "this is return"
        } 
        Function.prototype.myBind = function(){
            let thts = this
            let arg = Array.prototype.slice.call(arguments)
            let _this = arg.shift(arg)
            return function(){
                thts.apply(_this,arg)
            }
        }

        let cd = fun.myBind({x:'103333'},1,2,3)
        cd()

闭包

什么是闭包

闭包是作用域的一种特殊应用

触发闭包的几种情况

  1. 函数作为参数传递
  2. 函数作为返回值返回
  3. 自执行匿名函数

闭包的应用

  1. 隐藏变量
  2. 解决for i 问题

自由变量

  1. 不在自己作用域的变量叫做自由变量
  2. 自由变量的值:在函数定义的上级作用域去找,不是在调用的地方

evenl loop的机制

同步代码执行完之后,eventloop开始时间轮回,循环执行异步代码

宏任务、微任务

什么是宏任务,什么是微任务

  1. 红任务 : setTiemout setInterval Ajax Dom时间
  2. 微任务: promise async await

执行顺序

  1. 同步代码
  2. 微任务
  3. 渲染Dom
  4. 红任务