手写 instanceof

    1. let toString = Object.prototype.toString.bind()
    2. function myInstanceof (object, constructor) {
    3. let tag = toString(constructor)
    4. if (
    5. tag === '[object Boolean]' ||
    6. tag === '[object String]' ||
    7. tag === '[object Number]' ||
    8. tag === '[object Undefined]' ||
    9. tag === '[object Null]' ||
    10. tag === '[object Symbol]' ||
    11. tag === '[object BigInt]' ||
    12. ) {
    13. throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not an object`)
    14. }
    15. if (tag === '[object Array]' || tag === '[object Object]') {
    16. throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not callable`)
    17. }
    18. let proto = Object.getPrototype(object)
    19. let prototype = constructor.prototype
    20. while(proto) {
    21. if (proto === prototype) {
    22. return true
    23. }
    24. proto = Object.getPrototype(proto)
    25. }
    26. return false
    27. }

    1.介绍下 BFC、IFC、GFC 和 FFC
    BFC(block formatting contexts):块级格式化上下文
    一般用于 block 元素布局,可以单独隔离一个 html 渲染区域出来,不如说本质上 html 最外层就是块级格式化上下文,display: inline-block/flow-root,position、float、overflow 都可以创建
    IFC(inline formatting contexts):行级格式化上下文 / 内联格式化上下文
    一般用于 inline 元素布局
    GFC(grid formatting contexts):网格布局格式化上下文
    一般用于 grid 布局
    FFC(flex formatting contexts):自适应格式化上下文
    一般用于 flex布局

    2.有以下 3 个判断数组的方法,请分别介绍它们之间的区别和优劣(1. Object.prototype.toString.call(),2. instanceof,3. Array.isArray())

    1. toString
      1. 调用栈很长,不太优雅,
      2. 不能判断类数组?
    2. instanceof
      1. 会错误判断一些继承自数组的方法,比如把类数组判断为数组
      2. 不能跨 iframe 判断(因为 iframe 里的其实是两个 Realme 下的构造函数)
    3. Array.isArray
      1. 不能判断类数组?
      2. Array.prototype 其实也是一个数组
      3. 可以跨 iframes 判断
      4. pollyfill 其实就是 toString

    3.下面代码中 a 在什么情况下会打印 1?

    1. var a = ?;
    2. if(a == 1 && a == 2 && a == 3){
    3. console.log(1);
    4. }
    5. let a = {
    6. val: 1,
    7. toString: function () {
    8. return this.val++
    9. }
    10. }