What exactly does JS reference type do in implicit type conversion

1 Introduction

In actual development, both comparison operators and congruence operators are frequently applied points.
In the case of different types on both sides, the comparison operator obtains the comparison result after implicit type conversion.

However, there are some pitfalls in the implicit type conversion of reference types. In actual development, a little carelessness will cause bugs
such as:

[] == 0 // true

So it’s good to understand what the bottom layer does

2 Conversion of reference types and basic data types

In implicit conversion, the reference type is generally converted to the basic data type, and then the comparison is performed.
The purpose of the conversion is only one: to obtain the basic data type, or the original value

Two APIs are generally used when converting reference data types: valueOf and toString

  • The valueOf method is used to convert the object to the original value. If the object has no original value, valueOf will return the object itself. You rarely need to call the valueOf method yourself; JavaScript will automatically call it when it encounters an object with the expected original value
  • The toString method returns a string representing the object

2.1 Object to string

Rewrite the prototype's valueOf and toString for a simple verification. Time is limited, and the logic may not be completely rigorous. If you have time, you can write more cases according to your ideas.

const log = console.log
Object.prototype.toString = () => 'a'
Object.prototype.valueOf = () => 'b'
log(Object() == 'a') // false
log(Object() == 'b') // true

Object.prototype.toString = () => 'a'
log(Object() == 'a') // true

Call valueOf first and then toString

2.2 Object to number

Object.prototype.toString = () => 1
Object.prototype.valueOf = () => 2
log(Object() == 1)
log(Object() == 2) // true

Call valueOf first and then toString

2.3 Array to String

Array.prototype.toString = () => 'a'
Array.prototype.valueOf = () => 'b'
log([] == 'a') // false
log([] == 'b') // true

Call valueOf first and then toString

Array.prototype.join = () => 'a'
log([] == 'a') // true

The underlying call is join

At this point, it is easy to understand that [] == 0 is true as mentioned at the beginning of the article

  1. It is detected that the reference type and the basic type are compared, so js implicitly converts the reference type to the basic type and then compares
  2. In implicit conversion, valueOf is called first, and because it is an array, the execution result of the join method is returned in valueOf
  3. The problem becomes familiar'' == 0, return true

3 summary

Reference type to basic type, the purpose is to get the original value, the bottom layer is to call valueOf first and then toString
process, if any method gets the original value, it will return, otherwise continue to the next method.
If the original value is never obtained, then Throw an exception

Guess you like

Origin blog.csdn.net/qq_41109610/article/details/114242676