[] == ![] es cierto por qué

 [] == ![] es cierto ¿por qué?

enlace de referencia del código fuente js

解释一、从第7条开始:typeof ![] 是一个 boolean 对象为 false。所以 [] == ![] 其实就是 [] == 0
[] == 0 就对应了第9条,然后 [] 需要转为字符串再转为数值,才能和 0 比较,所以又对应了第5条,'' == 0 => 0 == 0
所以最终结果为true

//或

解释二、首先计算 ![],[] 是一个正常的对象,是一个 truthy 值,所以 ![] 是 false

这样问题变成了如何比较 [] 和 false,因为它们不是一个类型,所以会先尝试转换到 number 类型。(JS 规范明确了双等号比较的流程)

false 转为 number 显然是 0

[] 先转为 string 才能转为 number,[] 转为 string 是空字符串 "",再转为 number 是 0

现在比较两个 number,都是 0,所以 [] == ![] 是 true

Este repositorio también contiene muchas cosas extrañas en JS y las explicaciones correspondientes.

Supongo que te gusta

Origin blog.csdn.net/weixin_46600931/article/details/129274532
Recomendado
Clasificación