How does string equality work in JavaScript?
There are plenty of tutorials for ==
and ===
so please don't guide me to a basic tutorial, my question is a bit more specific:
For example http://www.w3schools.com/jsref/jsref_obj_string.asp states that:
Syntax:
var txt = new String("string");
// or more simply:
var txt = "string";
Good, but what about this?
alert(new String("a") == new String("a")); // false
alert("a" == "a"); // true
var a = new String("a");
var b = new String("a");
alert(a == b); 开发者_如何学编程// false
var c = "a";
var d = "a";
alert(c == d); // true
alert(c === d); // true
alert (a === c); // false
Of course nobody calls new String()
anyway, but is it something about the equality failing because new String()
is handled as an object not as a string?
And of course W3Schools is not the most trusted source but I would have expected all of the above alerts to say true.
Please explain.
The "surprising results" come from the way Javascript handles equality for Objects, plus the confusion that arises between string literals and String objects. From the Mozilla reference guide for the ==
operator:
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
You can experience the same behavior with numbers:
new Number(5) == new Number(5) // false
And clarify your mind by:
typeof "string" // string
typeof new String("string") // object
String literals, which are primitive value types, are different from new String
objects, which are entities with distinct references wrapping those values. See Predefined Core Objects in Mozilla's JavaScript docs for details.
So you're right in that comparison is handled differently for literals and for objects, simply because one compares their values while the other compares references.
You are correct that in your example case you are comparing 2 different object references. Within the language specification you'll find this algorithm. The portion you are looking for is section 1 f.
11.9.3 The Abstract Equality Comparison Algorithm
11.9.3 The Abstract Equality Comparison Algorithm The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows: 1. If Type(x) is the same as Type(y), then a. If Type(x) is Undefined, return true. b. If Type(x) is Null, return true. c. If Type(x) is Number, then i. If x is NaN, return false. ii. If y is NaN, return false. iii. If x is the same Number value as y, return true. iv. If x is +0 and y is -0, return true. v. If x is -0 and y is +0, return true. vi. Return false. d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false. e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. f. Return true if x and y refer to the same object. Otherwise, return false. 2. If x is null and y is undefined, return true. 3. If x is undefined and y is null, return true. 4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y). 5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y. 6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y. 7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). 8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y). 9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y. 10. Return false.
Also take notice that steps 8 and 9 which makes dealing with String objects a bit cleaner.
alert(new String("a") == "a"); // true
alert("a" == new String("a")); // true
精彩评论