手写 instanceof
let toString = Object.prototype.toString.bind()function myInstanceof (object, constructor) {let tag = toString(constructor)if (tag === '[object Boolean]' ||tag === '[object String]' ||tag === '[object Number]' ||tag === '[object Undefined]' ||tag === '[object Null]' ||tag === '[object Symbol]' ||tag === '[object BigInt]' ||) {throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not an object`)}if (tag === '[object Array]' || tag === '[object Object]') {throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not callable`)}let proto = Object.getPrototype(object)let prototype = constructor.prototypewhile(proto) {if (proto === prototype) {return true}proto = Object.getPrototype(proto)}return false}
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())
- toString
- 调用栈很长,不太优雅,
- 不能判断类数组?
- instanceof
- 会错误判断一些继承自数组的方法,比如把类数组判断为数组
- 不能跨 iframe 判断(因为 iframe 里的其实是两个 Realme 下的构造函数)
- Array.isArray
- 不能判断类数组?
- Array.prototype 其实也是一个数组
- 可以跨 iframes 判断
- pollyfill 其实就是 toString
3.下面代码中 a 在什么情况下会打印 1?
var a = ?;if(a == 1 && a == 2 && a == 3){console.log(1);}let a = {val: 1,toString: function () {return this.val++}}
