一、if()中的布尔判断
if
( Expression ) 表达式会通过ES5定义的ToBoolean方法强制把Expression 转换成布尔值。
数据类型 | 转换结果 |
Undefined | false |
Null | false |
Boolean | 不变 |
Number | +0, ?0, or NaN 转换为false; 其他的转换为true. |
String | 空字符串或长度为0的字符串转换为false; 其他的转换为true. |
Object | true |
示例:
var trutheyTester = function(expr) { return expr ? "truthey" : "falsey"; } trutheyTester({}); //truthey (an object is always true) trutheyTester(false); //falsey trutheyTester(new Boolean(false)); //truthey (an object!) trutheyTester(""); //falsey trutheyTester(new String("")); //truthey (an object!) trutheyTester(NaN); //falsey trutheyTester(new Number(NaN)); //truthey (an object!)
二、==
会对不同类型的数据进行强制转换以比较,记住两点即可:
- undefined和null永远互相相等
- 当两个不同类型的值进行比较,Object会被toPrimitive()转化为原始值,String和Boolean会被toNumber()转换为数字或NaN
Type(x) | Type(y) | Result |
x和y是同一种数据类型 | 和===判断原则一样一样 | |
null | Undefined | true |
Undefined | null | true |
Number | String | x == toNumber(y) |
String | Number | toNumber(x) == y |
Boolean | (any) | toNumber(x) == y |
(any) | Boolean | x == toNumber(y) |
String or Number | Object | x == toPrimitive(y) |
Object | String or Number | toPrimitive(x) == y |
otherwise… | false |
toNumber()的转换规则:
形式参数类型 | 结果 |
Undefined | NaN |
Null | +0 |
Boolean |
true -->1; false-->+0; |
Number | 不变 |
String | “abc” -> NaN
“123” -> 123 |
Object |
1. Let primValue be ToPrimitive(input argument, hint Number). 2. Return ToNumber(primValue). |
ToPrimitive()转换规则:
形式参数 | 结果 |
Object | (在做相等判断进行强制转换时)先调用valueOf(),没有返回值再调用toString(),还没有返回值则报错。 |
otherwise… | 不变 |
示例:
[0] == true;
1 //EQUALITY CHECK... 2 [0] == true; 3 4 //HOW IT WORKS... 5 //convert boolean using toNumber 6 [0] == 1; 7 //convert object using toPrimitive 8 //[0].valueOf() is not a primitive so use... 9 //[0].toString() -> "0" 10 "0" == 1; 11 //convert string using toNumber 12 0 == 1; //false!
“potato” == true;
//EQUALITY CHECK... "potato" == true; //HOW IT WORKS... //convert boolean using toNumber "potato" == 1; //convert string using toNumber NaN == 1; //false!
“potato” == false;
//EQUALITY CHECK... "potato" == false; //HOW IT WORKS... //convert boolean using toNumber "potato" == 0; //convert string using toNumber NaN == 0; //false!
object with valueOf;
//EQUALITY CHECK... crazyNumeric = new Number(1); crazyNumeric.toString = function() {return "2"}; crazyNumeric == 1; //HOW IT WORKS... //convert object using toPrimitive //valueOf returns a primitive so use it 1 == 1; //true!
object with toString;
//EQUALITY CHECK... var crazyObj = { toString: function() {return "2"} } crazyObj == 1; //HOW IT WORKS... //convert object using toPrimitive //valueOf returns an object so use toString "2" == 1; //convert string using toNumber 2 == 1; //false!
三、===
值和类型都必须严格相等:
- 不同类型永远不相等
- 对象必须引用自同一个对象才相等
- 字符串必须包含相同的字符集合
- 原型(primitives)共享同一个值才相等
- NaN, null and undefined永远不和其他类型相等
- NaN甚至不会和自己相等
数据类型 | 值 | 结果 |
二者类型不同 | false | |
Undefined or Null | true | |
Number | 二者值相等,除了NaN | true |
String | 二者包含相同的字符 | true |
Boolean | 二者都是true或false | true |
Object | 二者引用自同一个对象 | true |
otherwise… | false |
参考资料:
1. 《Truth, Equality and JavaScript 》:https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/
时间: 2024-10-07 09:08:21