原文链接: https://interview.poetries.top/docs/excellent-docs/3-JS%E6%A8%A1%E5%9D%97.html
JavaScript
简版速记
面试前 5 分钟扫一遍的高频结论,细节看正文对应小节。
- 数据类型:7 种原始类型
undefined / null / boolean / number / string / symbol / bigint+ 引用类型Object。typeof null === 'object'是历史 Bug;NaN !== NaN。 - 类型检测:
typeof判原始类型(除 null)+ 函数;instanceof查原型链,判不了原始类型;最全用Object.prototype.toString.call(x)返回[object Xxx];判数组优先Array.isArray。 - 0.1 + 0.2 ≠ 0.3:IEEE 754 双精度二进制表示精度丢失;判等用
Math.abs(a-b) < Number.EPSILON。 - 类型转换:
==会隐式转换,===不会;对象转原始走Symbol.toPrimitive → valueOf → toString;[] == ![]为true。 - 闭包:函数 + 其引用的外部作用域;本质是「当前环境存在指向父级作用域的引用」。
for循环输出问题用let/ IIFE /setTimeout第三参解决。 - 原型链:
实例.__proto__ === 构造函数.prototype;构造函数.prototype.constructor === 构造函数;沿__proto__向上直到null。 - 继承:最推荐寄生组合继承
Child.prototype = Object.create(Parent.prototype)+ 修正constructor;ES6 用class … extends。 - this:优先级
new>bind/call/apply>obj.foo()> 普通调用;箭头函数无自身 this,取定义时外层第一个普通函数的 this,且不可被 bind 改变。 - 内存:原始类型存栈,对象存堆,赋值时原始拷值、对象拷引用地址;闭包变量存在堆中。
- Event Loop:一个宏任务 → 清空所有微任务 →(必要时渲染)。微任务
Promise.then / MutationObserver / queueMicrotask,宏任务setTimeout / setInterval / I/O。Node 把宏任务分 6 阶段、微任务额外有process.nextTick(优先级最高,node 11 后每个宏任务后清空微任务)。 - 深浅拷贝:浅拷贝
Object.assign/ 展开运算符只复制一层;深拷贝可用structuredClone(现代)或递归 /JSON.parse(JSON.stringify())(有局限)。
