数据类型之隐式转换(console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]))

mac2025-09-30  11

数据类型之隐式转换

两类隐式转换

x == y

如果 x,y 的类型相同,则返回 x == y如果 x 为 undefined,y 为 null,或者, x 为 null,y 为 undefined,都返回 true如果 x,y 都为基础数据类型,但类型不同,返回 Number(x) == Number(y)如果一者为基础数据类型,一者为对象,则返回 ToPrimitive(obj) == primitive

ToPrimitive(obj),先调用 obj.valueOf(),判断是否是基础数据类型,若不是,则调用 obj.toString()

[] == ![] => [] == !Boolean([]) => [] == !true => [] == false => "" == false => true console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]) ([][[]]+[]) ([][[]]+[]) => ([][""]+[]) => (undefined + []) => ("undefined") [+!![]] [+!![]] => [+!!Boolean([])] => [+!!true] => [+true] => [+1] => [1] ([][[]]+[])[+!![]] => ("undefined")[1] => "n" ([]+{}) ([]+{}) => ("[object object]") [!+[]+!![]] [!+[]+!![]] => [!+[]+true] => [!+"" + true] => [!+0+true] => [!0+true] => [1+true] => [2] 那么 ([]+{})[!+[]+!![]] => ("[object object]")[2] => "b"

综上

console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]) // nb

拓展

console.log((!(~+[]) + {})[--[~+""][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]]) //sb console.log((![]+"")[!+[]*(!+[]+!![])]+(([]+{})[!+[]+!![]])) //lb
最新回复(0)