JavaScript's equal sign judgment seems a little difficult to understand, and many people are a little dizzy. Let's take a look:
there are two equal JavaScript judgments: 1) = 2) =
== (full)
let's look at the three equal signs first. Those who have read the Javascript authoritative guide have a deep understanding. In my own understanding, if both sides are of the reference type, true is returned if both sides point to the same element, otherwise, false is returned. If one of them is of the reference type and the other is not, false is returned. If both the left and right sides are simple types and the values are the same, true is returned.
special: undefined and other undefined
This seems easy to understand. Let's take a look at the following example:
alert (0 = 0); // true
var OBJ = {};
var obj1 = OBJ; // obj1 copies the reference of obj.
alert (obj1 = OBJ) // true
alert ({}=={}) // No two objects are equal or incomplete
= (Equal)
The same rule is a bit complicated:
- If the type of an operand is Boolean, convert it to numeric type first, convert false to 0, and convert true to 1.
- If one of the operands is a string and the other is a number, convert the string to a number for comparison.
- If one of the operands is a string and the other is an object, the string will be compared after the tostring method of the object is called.
- If one of the operands is numeric and the other is object, convert the object to a numeric value and compare the numbers.
Special:
- Null and undefined are equal.
- Null and undefined are not converted to any other type.
- If the result of any operation is Nan, if the comparison is equal, false is returned. If the comparison is not equal, true is returned. Note: Even if both operands are Nan, the returned result is false, that is, Nan is not equal to Nan.
- If both operands are objects, compare the referenced values. If the same object is referenced, true is returned. Otherwise, false is returned.
Although the details are written, it is still a little difficult to use. Let's take a look at the following situations:
1) determine whether a variable is assigned a value in the function.
Generally, the value of a parameter or variable defined by VAR is undefined without being assigned a value. to judge the value, you must write it as follows:
Function Test (p ){
If (P = undefined ){
Alert ("P value not passed ")
}
}
Of course, if the caller directly writes test (undefined), I can't help it.
2) use! -[1,] IE8 and the following browsers
First, I am not sure whether this method is a reliable one. This kind of hack method does not know when MS will fix a patch. After all, people do not directly say this is a feature of others.
Write if (! -[1,]) actually corresponds to If (! -[1,] = false) is a concept. The first entry of the Equality determination rule, both left and right are converted into numbers. If the error occurs, Ms does not fully interpret the array initialization definition in ecma262. For details, see [1] In sequence [1,]. however, IE8 and the following are interpreted as [1, undefined]. [1]. tostring () returns "1" and [1, undefined]. tostring () returns "1 ,". Then there is the-number. In this case, the "1" or "1," must be converted into a number, so the expression in IE8 and below will be changed :! (0-number ("1,") = "! (0-Nan) =>! Nan => true while other browsers explain :! (0-number ("1") =>! (0-1) =>! -1 => false.
3) about! [] = []
! [] = [] Run the expression on the left to get false, and then false = []. Similarly, both sides of the first rule need to be converted into numbers: Number (false) = Number ([]). In this way, 0 = 0 is returned.
See here: http://www.xdarui.com/index.php/archives/131