= =
,= = =
和 Object.is()
1, the difference between
= =
If the two values have different types, perform type conversion first and then compare= = =
Direct value comparison without type conversionObject.is(val1, val2)
Determines whether two values are the same
2,= =
How do type conversions work?
- If the type is different, cast it
- Determine if you are comparing
null
Or is itundefined
If so, returntrue
- Check whether the type is
string
ornumber
, if so, willstring
convertnumber
- Determine if either party is
boolean
If so, convert one of the parties tonumber
Judging - Determine if either party is
object
And the other party isstring
,number
,symbol
, if so, willobject
To determine the original type (valueOf()
Methods) - If one of them is
NaN
, directly returnsfalse
- If both are objects, it compares whether they refer to the same object
3,[] = =! []
What is the value of?
Answer: True
The transformation steps
- ! The operator has the highest precedence,
! []
Will be converted tofalse
, so at this time is[] == false
- According to Article 4, one of the parties is
boolean
,boolean
tonumber
, so at this time is[] = = 0
- And then according to number five, put the array
[]
To the original type, call the arraytoString()
Method,[].toString() = ''
, so at this time is' '= = 0
- And according to rule number three, the
string
tonumber
.' '
tonumber
Phi is zero, so at this point0 = = 0
- The data type on both sides is the same
0 = = 0
fortrue
4, object.is () check if two values are equal
No cast is performed
- Are all
undefined
- Are all
null
- Are all
true
或false
- Are strings of the same length and the same characters are arranged in the same order
- Are the same objects (meaning each object has the same reference)
- It’s all numbers and
- Are all
+ 0
- Are all
0
- Are all
NaN
- Or both are non-zero and not
NaN
And are the same value
- Are all