A Roadmap
  • Introduction
  • Roadmap
  • Basics
    • Basic Terminal Usage
      • Shell
        • 基本
        • 变量
        • 传递参数
        • 运算符
        • 命令
        • 流程控制
        • 函数
        • 输入输出重定向
    • Character Encoding
      • 字符编码
    • Data Structures & algorithms
    • Network
      • TCP/IP
      • Http/Https
        • 浏览器缓存
        • Status Code
        • GET & POST
        • HTTPS 握手过程
        • HostOnly Cookie
      • CDN
        • CDN 工作原理
    • Version Control
      • git message format
      • git commands
    • Principles
    • Design Patterns
    • Others
      • JSON
      • 正则表达式
  • Front End
    • Web Standard
      • Html
      • CSS
        • font-face 小记
        • Grid 布局简易笔记
      • Javascript
        • Ajax
          • Fetch
          • XMLHttpRequest
        • DOM
          • Selection
          • 常用 DOM 操作
          • script 标签的几个属性
        • Ecmascript
          • this 关键字
          • Event Loop
          • 隐式转换
          • Date
        • Event
          • 模拟事件
          • Others
    • Development
      • Package Management
        • npm
      • Modulization
        • 模块化机制
        • webpack 打包解析
      • Architecture
      • Build Tools
        • 如何编写一个自定义的 eslint 规则?
      • Debug
        • 移动端调试 web
      • Pre/Post Processors
      • Test
        • Jest Snapshot 使用指南
      • Type Checkers
        • TS 中 enum 的编译结果
    • Libs & frameworks
    • Platforms
      • Browser
        • Basics
          • 从输入 URL 到页面加载完成都发生了什么事情?
          • HTML 加载的过程示意图
          • 为什么读取某些属性,也会导致回流?
          • Chrome 进程模型
        • PWA
        • Compatible
        • Cross Origin
        • Performance
          • 性能检测:performance 对象
          • 性能指标
        • Others
          • 移动端 web 开发笔记
      • Server
      • Desktop Applications
      • Mobile Applications
        • Flutter 在移动端和 Web 端的技术实现
  • Back End
    • outline
    • Languages
      • Node
        • Event Loop
        • NodeJS 中的进程与线程
        • NodeJS 中的 esModule 与 commonJS
  • Clients
    • outline
    • 安装 IPA 包
  • DevOps
    • Languages
    • OS Concepts
    • Servers & terminal
  • UI & UX
    • outline
  • Others
    • Posts
      • 使用 node 爬取数据并导出到 excel
      • antd 1.x datepicker 时区问题
      • babel-transform-runtime 踩坑记录
      • lodash 按需加载注意事项
      • 记一次项目迁移的踩坑记录
      • 时区与JS中的Date对象
      • 记一次 vue + ts 开发踩坑
    • Tools
      • 个人常用的工具分享
      • tmux 简要笔记
Powered by GitBook
On this page
  • ToPrimitive
  • 数学运算
  • 比较运算
  • valueOf(MDN)
  • Relational and Equality Operators
  • What goes through If statement

Was this helpful?

  1. Front End
  2. Web Standard
  3. Javascript
  4. Ecmascript

隐式转换

PreviousEvent LoopNextDate

Last updated 3 years ago

Was this helpful?

ref:

Update 2019-04-23 摘自 tc39 ecma262 规范:

7.2.14 Abstract Equality Comparison

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

1. If Type(x) is the same as Type(y), then
Return the result of performing Strict Equality Comparison x === y.
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y).
5. If Type(x) is String and Type(y) is Number, return the result of the comparison ! ToNumber(x) == y.
6. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.
7. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).
8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
10. Return false.

ToPrimitive

函数签名:

ToPrimitive(input, PreferredType?) // PreferredType: Number 或者 String

流程如下:

  • input为原始值,直接返回;

  • 不是原始值,调用该对象的valueOf()方法,如果结果是原始值,返回原始值;

  • 调用valueOf()不是原始值,调用此对象的toString()方法,如果结果为原始值,返回原始值;

  • 如果返回的不是原始值,抛出异常TypeError。

其中PreferredType控制线调取valueOf()还是toString()。

ps: Date类型按照String去调用。

如果对prototype上的valueOf或者toString方法进行了修改,则隐式转换会有不一样的结果。可以通过以下代码看下上述流程是如何进行的。

[] == false; // true

Array.prototype.valueOf = () => true;
[].valueOf(); // true;
[] == false; // false

Array.prototype.toString = Object.prototype.toString;
[].toString(); // [object Array]
[] == false; // false

数学运算

  • 计算两个操作数的原始值: prima = ToPrimitive(a), prima = ToPrimitive(b);

  • 如果原始值有String,全部转换为String,返回String相加后的结果;

  • 如果原始值没有String,全部转换为Number, 返回Number相加后的结果;

比较运算

== 中的隐式转换:

  • x y都为Null或undefined,return true;

  • x或y为NaN, return false;

  • 如果x和y为String,Number,Boolean并且类型不一致,都转为Number再进行比较

  • 如果存在Object,先转换为原始值,再进行比较

Relational and Equality Operators

What goes through If statement

valueOf()

JS的隐式转换 从 [] == false 说起
tc39 - spec - abstract equality comparison
MDN