news 2026/5/29 0:49:59

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
隐式类型转换:哈基米 == 猫 ? 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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/28 18:48:26

AppFlowy如何实现多设备无缝同步?揭秘分布式协作技术架构

AppFlowy如何实现多设备无缝同步&#xff1f;揭秘分布式协作技术架构 【免费下载链接】AppFlowy AppFlowy 是 Notion 的一个开源替代品。您完全掌控您的数据和定制化需求。该产品基于Flutter和Rust构建而成。 项目地址: https://gitcode.com/GitHub_Trending/ap/AppFlowy …

作者头像 李华
网站建设 2026/5/28 6:48:37

Kotaemon语音播报功能:TTS合成回答内容

Kotaemon语音播报功能&#xff1a;TTS合成回答内容 在智能客服、企业知识库和虚拟助手日益普及的今天&#xff0c;用户早已不再满足于“一问一答”的文本交互。他们希望系统不仅能“读懂问题”&#xff0c;还能“张口回答”——就像一位随时在线的真人助理&#xff0c;用自然的…

作者头像 李华
网站建设 2026/5/28 22:48:10

FanControl终极配置指南:从零开始打造完美散热系统

FanControl终极配置指南&#xff1a;从零开始打造完美散热系统 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/fa/Fa…

作者头像 李华
网站建设 2026/5/28 23:58:14

Windows Terminal扩展能力深度解析:从基础配置到高阶定制

Windows Terminal扩展能力深度解析&#xff1a;从基础配置到高阶定制 【免费下载链接】terminal The new Windows Terminal and the original Windows console host, all in the same place! 项目地址: https://gitcode.com/GitHub_Trending/term/terminal 还在为命令行…

作者头像 李华
网站建设 2026/5/28 2:53:20

想让故障修复时间缩短80%?先掌握这3个应急响应环节

在数据驱动的时代&#xff0c;数据库已成为IT系统的核心命脉。但残酷的现实是&#xff0c;75%的严重业务中断源于未被及时发现的数据库隐患&#xff0c;超过60%的数据库故障因未能提前预警而升级为严重事故。想要将故障平均修复时间缩短80%&#xff0c;关键在于建立一套高效的应…

作者头像 李华