• JS中的 typeof 针对各种类型的返回值 以及typeof历史遗留问题


    JS中的 typeof 针对各种类型的返回值

    typeof 运算符返回一个字符串,表示操作数的类型。

    下表总结了 typeof 可能的返回值

    类型结果
    Undefined"undefined"
    Null"object"原因
    Boolean"boolean"
    Number"number"
    BigInt"bigint"
    String"string"
    Symbol"symbol"
    Function(在 ECMA-262 中实现 [[Call]];classes也是函数)"function"
    其他任何对象"object"

    测试

    // 数值
    typeof 37 === "number";
    typeof 3.14 === "number";
    typeof 42 === "number";
    typeof Math.LN2 === "number";
    typeof Infinity === "number";
    typeof NaN === "number"; // 尽管它是 "Not-A-Number" (非数值) 的缩写
    typeof Number(1) === "number"; // Number 会尝试把参数解析成数值
    typeof Number("shoe") === "number"; // 包括不能将类型强制转换为数字的值
    
    typeof 42n === "bigint";
    
    // 字符串
    typeof "" === "string";
    typeof "bla" === "string";
    typeof `template literal` === "string";
    typeof "1" === "string"; // 注意内容为数字的字符串仍是字符串
    typeof typeof 1 === "string"; // typeof 总是返回一个字符串
    typeof String(1) === "string"; // String 将任意值转换为字符串,比 toString 更安全
    
    // 布尔值
    typeof true === "boolean";
    typeof false === "boolean";
    typeof Boolean(1) === "boolean"; // Boolean() 会基于参数是真值还是虚值进行转换
    typeof !!1 === "boolean"; // 两次调用 !(逻辑非)运算符相当于 Boolean()
    
    // Symbols
    typeof Symbol() === "symbol";
    typeof Symbol("foo") === "symbol";
    typeof Symbol.iterator === "symbol";
    
    // Undefined
    typeof undefined === "undefined";
    typeof declaredButUndefinedVariable === "undefined";
    typeof undeclaredVariable === "undefined";
    
    // 对象
    typeof { a: 1 } === "object";
    
    // 使用 Array.isArray 或者 Object.prototype.toString.call
    // 区分数组和普通对象
    typeof [1, 2, 4] === "object";
    
    typeof new Date() === "object";
    typeof /regex/ === "object";
    
    // 下面的例子令人迷惑,非常危险,没有用处。避免使用它们。
    typeof new Boolean(true) === "object";
    typeof new Number(1) === "object";
    typeof new String("abc") === "object";
    
    // 函数
    typeof function () {} === "function";
    typeof class C {} === "function";
    typeof Math.sin === "function";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    typeof 历史遗留问题

    在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于 null 代表的是空指针(大多数平台下值为 0x00),因此,null 的类型标签是 0,typeof null 也因此返回 “object”。(参考来源)

    曾有一个 ECMAScript 的修复提案(通过选择性加入的方式),但被拒绝了。该提案会导致 typeof null === ‘null’。

  • 相关阅读:
    react中怎么为props设置默认值
    python 基本语法(二)
    【前端】HTTP相关知识总结
    C++ //练习 9.16 重写上一题的程序,比较一个list<int>中的元素和一个vector<int>中的元素。
    【Mybatis 使用】mybatis-config.xml 配置(properties 和 settings)
    ATK-ESP8266使用说明(STM32-F4)
    抓的是周树人,与我鲁迅有什么关系?
    从0开始学习JavaScript--JavaScript 流程控制
    The Grapes NFT 概览与数据分析
    blender 常用修改器
  • 原文地址:https://blog.csdn.net/sinat_51673411/article/details/133521712