隐式类型转换:哈基米 == 猫 ? true :false

何意味?

如果我们以往只接触过一门强类型的编程语言,估计早已经皱起眉头,大呼一声"何意味?"。但是在js的世界中,一切是自由的。你甚至可以直接写一份 数字+引用数据类型 的代码而不报错。使得上段代码不报错的原因,其实是js在执行过程中发生了隐式类型转换隐式类型转换。

类型转换的规则

原始类型原始类型原始类型之间的转换

  • String =>Number: 字符串内部只能包含"-""+"符号,否则转换值为NaN

    console.log(Number("-123"))//只允许开头字符为"-+",其它都为数字,则转换为数字
    console.log(Number("123a")) // 字符串内部包含非数字字符,则转换为NaN

  • Boolean =>Number:true =>1 | flase =>0

    console.log(Number(true))
    console.log(Number(false))

  • Number,Boolean =>String : 直接加上""变成字符串

    console.log(String(123),typeof(String(123)))
    console.log(String(true),typeof(String(true)))

  • String => Boolean: 非空则为true,否则为false

    console.log(Boolean(""))
    console.log(Boolean("adadaw"))

  • Number => Boolean: 0则为false,否则为true

    console.log(Boolean(0))
    console.log(Boolean(-1))

引用类型 引用类型=> 原始类型的转换

  1. Toprimitive(obj) 这是js引擎内置的函数,功能是将引用类型转换为原始类型,我们没有办法直接调用,但是我们可以模拟一下它的内部实现

    其内部实现
    toprimitive(obj)
    {
    if(typeof (obj.valueOf())!='object') {
    return obj.valueOf();
    }
    else if(typeof (obj.toString())!='object') {
    return obj.toString();
    }

    复制代码
     return "报错:未知类型错误";

    }

  • 关于valueOf( ): 某些特殊的类型在设计之初已经设置好了,当该类型参与运算时,应当返回哪些类型的值,如果返回的是一个原始类型值,我们可以将其抛出,作为引用类型对象 => 原始类型 的值
  • 关于toString(),几乎所有类型的对象都内置了toString方法,以字符串的形式展示数据,我们可以将其抛出,作为引用类型对象 => 原始类型 的值。
  • 综上所述:引用类型 => 原始类型 的结果是可以预期的, 如果内置的valueOf()函数定义了,使用原始类型展示本对象数据的方式,则直接抛出,否则抛出大概率是执行toString()后抛出的字符串,不然就是报错了。

隐式类型转换

何时发生?

当运算符两端数据类型不同时就会发生。

  1. 四则运算
  • 在我们使用"+-x%"进行四则运算时

    // 会隐藏式的将引用类型转换为原始类型

    // 除"+"以外的运算时
    // 原始类型 - 原始类型 => number - number
    // 引用类型 - 引用类型 => toprimitive() - toprimitive() => number -number
    // 原始类型 - 引用类型 => number - toprimitive() => number -number

    // 执行"+"法运算时
    // 原始类型(除string外) + 原始类型(除去string 外) => number + number
    // string + 原始类型 => string + string
    // string + 引用类型 => string + 引用类型.toString()
    // 引用类型 + 原始类型=> 引用类型.toString() + string

    //例子
    console.log([12]+3)
    // [12].toString =>'12'
    //'12'+ 3 => '12' + '3' = '123'

    //例子
    console.log(new Date()-3)
    // toprimitiev(new Date()) => 1765874339393
    // 1765874339393 -3 =>1765874339390

2.关系运算

  • 在我们使用">,>=,<,<="进行关系运算时

    // 原始类型 >= 原始类型(除string外) => number >= number
    // stirng >= string => 按位比较Unicode码

    // 关系运算时,会先将引用类型隐式转换为原始类型
    // string >= 引用类型 => string >= toprimitive()
    // boolean >= 引用类型 => number >= toprimitiev()
    // number >= 引用类型 => number >= toprimitive()
    // 引用类型 >= 引用类型 => toprimitive() >= toprimitive()

    // 例子
    console.log([11]> 10)
    // toprimitiev([11])=> '11' ,Number('11') => 11
    // 11 > 10 => true

3.条件判断

  • 在我们使用"if,while,do while,!,? "条件判断时,会尝试将引用类型转换为boolean型,虽然会尝试将引用类型转换为原始类型,但是不改变最终结果为true

    条件判断时,不会将引用类型转换为原始类型
    //在进行条件判断时
    // 原始类型 => boolean
    // 引用类型 => boolean 且必为true

    // "!" 的使用,布尔值取反
    // !true => false
    // !false => true

    //例子
    console.log(![]) // false

    // ![] 向boolean转换。
    // Boolean([]) => true
    // !true => false

4.相等运算

  • 在我们使用"=="进行判断运算时

    // 原始类型 == 原始类型(除去string) => number == number
    // string == string => 按位比较Uicode码
    // stirng == 引用类型 => string == 引用类型.toString() => string == string
    // number == 引用类型 => number == toprimitive() => number >= number
    // boolean == 引用类型 => number == toprimitive() => number >= number
    // 引用类型 == 引用类型 => 直接比较地址,不进行隐式转换

    //例子
    console.log([]==![]) // true

    // ![] 先向boolean转换,再转为数字。
    // Boolean([]) => true , !true => false

    // [] == flase (引用类型 == 布尔类型)
    //Number(false) => 0
    //toprimitive([])=>'', Number('') => 0
    // 0 == 0 => true

    console.log([]==[]) // false

相关推荐
saoys31 分钟前
Opencv 学习笔记:创建与原图等尺寸的空白图像
笔记·opencv·学习
king王一帅2 小时前
Incremark Solid 版本上线:Vue/React/Svelte/Solid 四大框架,统一体验
前端·javascript·人工智能
C雨后彩虹5 小时前
任务最优调度
java·数据结构·算法·华为·面试
晓幂6 小时前
【2025】HECTF
笔记·学习·web安全
SmartRadio7 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
rfidunion7 小时前
QT5.7.0编译移植
开发语言·qt
rit84324997 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
智航GIS7 小时前
10.4 Selenium:Web 自动化测试框架
前端·python·selenium·测试工具
慕云紫英7 小时前
基金申报的一点经验
学习·aigc
微露清风7 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习