[js] “===“ 及 typeof
是 JavaScript 的严格相等运算符和 宽松相等有本质区别运算符行为会尝试类型转换后再比较值和类型都相等才为 true不做类型转换在本例中typeof window.drawPolyline functiontypeof 的返回值永远是字符串‘function’、‘undefined’、‘object’ 等和字符串 ‘function’ 比较 直接比对类型一致、值一致如果写成 效果相同因为两边都是 string 类型。但 是 JS 社区推荐做法——显式声明我不需要类型转换避免埋坑。举个典型的坑00// true ← 字符串 0 被转成数字 000// false ← 类型不同直接 false所以在生产代码中统一用 是好习惯——明确、可预测typeof 返回值表共 8 种返回值触发条件示例“undefined”未声明 / 已声明未赋值 / 显式赋 undefinedtypeof x、typeof undefined“boolean”布尔值typeof true、typeof false“number”数字含 NaN、Infinitytypeof 42、typeof NaN、typeof Infinity“bigint”BigInt 大整数typeof 1n、typeof BigInt(42)“string”字符串typeof “hello”、typeof ‘’“symbol”Symbol 唯一值typeof Symbol()、typeof Symbol.iterator“function”函数含 classtypeof function(){}、typeof class {}、typeof Math.max“object”对象、数组、null、正则……typeof {}、typeof []、typeof null、typeof /a/关键判断逻辑typeof 本质上取的是 JS 引擎内部的 [[Type]] 标签不是原型链推断值.内部类型标签 ──→ typeof 返回值三个著名陷阱① null 返回 “object”typeofnullobject// true ← JS 历史 bug永远如此检查 null 必须用xnull② 数组返回 “object”typeof[1,2,3]object// true检查数组用Array.isArray([1,2,3])// true③ NaN 返回 “number”typeofNaNnumber// true ← NaN 的类型是数字typeof NaN “number” // true ← NaN 的类型是数字Number.isNaN(x)undefined → “undefined”null → “object” ← 陷阱true/false → “boolean”数字/NaN → “number”1n → “bigint”“str” → “string”Symbol() → “symbol”function → “function” ← 唯一的非 “object” 引用类型其它全 → “object” ← 对象、数组、正则、null 都落这里