js部分

js 基础

  1. 基本类型

    1. 原始类型(值传递)
      1. string
      2. number
      3. null
      4. undefined
      5. boolean
      6. symbol
    2. 对象(地址传递)
    3. 函数(地址传递)
  2. 类型判断

    1. typeOf
      1. 除了 null 其他都能是正确类型
      2. 除原始类型以及函数外都是 object
    2. instanceOf
      1. 不能正确处理原始类型
      2. 原型连
      3. 不能准确判断类型
  3. 类型转换

    1. 转换布尔值
      1. 除 null undefined 空窜 0 false NaN 都是 true
    2. 转换数字
      1. 空数组 0 数组只有一个数值 转换为数值
      2. 空字符串 0
      3. 对象 转化为 NaN
      4. “1”–> 1 Object –> NaN true –> 1 false –> 0 Null –> 0 “any”–> NaN
    3. 转换字符串
      1. Object –> [Object Object] Array –> Array.join(‘,’)
    4. 对象转换为 原始类型
      1. 判断 valueOf() 是否转化为原始类型 如果不是调用 toString() 方法
      2. 所以 空数组转换为 0 情况
    5. 加法转换
      1. 如果一方为字符串 另一方也会转换为字符串
      2. 如果不是字符串或则数值会通过 转化为原始类型顺序处理
    6. 比较转换
      1. === 基础类型不进行转换 直接比较,任意一个条件不满足就是不同
      2. == 进行以下转换(转换为相同类型再进行比较)
        1. 都是对象则地址比较
        2. 一方为原始类型则另一方转换为原始类型比较
        3. 编码比较 unicode
  4. this

    1. 指向调用函数
    2. 指向apply函数
    3. 指向bind函数(只能绑定一次)返回闭包
  5. 闭包

    1. 一个函数可访问另一函数作用域内的局部变量
  6. 拷贝

    1. 深拷贝
      1. JSON处理

        1. 因为转换成字符串,所以 undefined function symbol 无法复制 循环引用处理不来
      2. MessageChannel 可以处理不包含函数的 对象深拷贝

        1
        2
        3
        4
        5
        6
        7
        8

        function deepClone(obj){
        return new Promise((resolve)=>{
        const {port1, port2} = new MessageChanel()
        port1.onmessage = msg=> resolve(msg.data)
        port2.postMessage(obj)
        })
        }
      3. deepCopy

    2. 浅拷贝(只拷贝一层)
      1. Object.assign() 展开符
      2. Array.slice()
  7. 原型链

    1. 多个对象通过__proto__ 链接起来
    2. child.proto = 构造函数.prototype = 原型;原型.proto = 父级原型;原型.constructor = 构造函数
    3. 原型的 constructor 属性指向构造函数,构造函数又通过 prototype 属性指回原型
    4. Function.proto.constructor === Function === Function.prototype
    5. 对象里有_proto_这个属性。它指向原型,由它连接对象和原型组成原型链,同时原型里又有prototype属性,该属性里有一个constructor又指向回对象的构造函数形成一个闭环
  8. 模块化

    1. 优点
      1. 解决命名冲突
      2. 提高复用性
      3. 提高可维护性
    2. amd
      1. 依赖前置
      2. 先加载所有模块
    3. cmd
      1. 依赖后置
      2. 需要时引用
    4. CommonJS
      1. module.exports === exports
      2. exports 是module.exports的引用,不可直接赋值
  9. setInterval通常来说不

    1. 建议使用 setInterval。第一,它和 setTimeout 一样,不能保证在预期的时间执行任务。第二,它存在执行累积的问题,请看以下伪代码
  10. 事件循环

    1. macroTask(jobs)
      1. 宏任务 script , setTimeout ,setInterval ,setImmediate ,I/O ,UI rendering
    2. microTask(task)
      1. 微任务 process.nextTick ,promise ,MutationObserver
    3. 6个阶段
      1. timer 执行 setTimeout 与 setInterval
      2. io 执行 I/O 操作
      3. idle, prepare
      4. poll
        1. 回到 timer 阶段执行回调
        2. 执行 I/O 回调
  11. 手写bind

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function bind(){
    const _this = this
    const args = Array.from(arguments).slice()
    return function F(){
    if(this instanceOf F){
    return new _this(...args, ...arguments)
    }
    _this.apply(args.shift(), args.concat(arguments))
    }
    }