你真得懂Javascript中的==等于运算符吗?
2017-07-02 17:39
369 查看
var i = 2;
Number.prototype.valueOf = function() {
return i++;
};
var a = new Number( 42 );
if (a == 2 && a == 3) {
console.log( "Yep, this happened." );
}
============================
"0" == null; // false
"0" == undefined; // false
"0" == false; // true -- UH OH!
"0" == NaN; // false
"0" == 0; // true
"0" == ""; // false
false == null; // false
false == undefined; // false
false == NaN; // false
false == 0; // true -- UH OH!
false == ""; // true -- UH OH!
false == []; // true -- UH OH!
false == {}; // false
"" == null; // false
"" == undefined; // false
"" == NaN; // false
"" == 0; // true -- UH OH!
"" == []; // true -- UH OH!
"" == {}; // false
0 == null; // false
0 == undefined; // false
0 == NaN; // false
0 == []; // true -- UH OH!
0 == {}; // false
[] == ![]; // true
2 == [2]; // true
"" == [null]; // true
0 == "\n"; // true
var a = null;
var b = Object( a ); // same as `Object()`
a == b; // false
var c = undefined;
var d = Object( c ); // same as `Object()`
c == d; // false
var e = NaN;
var f = Object( e ); // same as `new Number( e )`
e == f; // false
======================================================
ES5规范中的解释:
Comparing: objects to nonobjects
If an object/function/array is compared to a simple scalar primitive
(string, number, or boolean), the ES5 spec says in clauses
11.9.3.8-9:
1. If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
2. If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Comparing: nulls to undefineds
Another example of implicit coercion can be seen with == loose
equality between null and undefined values. Yet again quoting the
ES5 spec, clauses 11.9.3.2-3:
1. If x is null and y is undefined, return true.
2. If x is undefined and y is null, return true.
Comparing: anything to boolean
1. If Type(x) is Boolean, return the result of the comparison
ToNumber(x) == y.
2. If Type(y) is Boolean, return the result of the comparison x ==
ToNumber(y).
Comparing: strings to numbers
1. If Type(x) is Number and Type(y) is String, return the result of
the comparison x == ToNumber(y).
2. If Type(x) is String and Type(y) is Number, return the result of
the comparison ToNumber(x) == y.
==============================直观的比較图
http://dorey.github.io/JavaScript-Equality-Table/
Number.prototype.valueOf = function() {
return i++;
};
var a = new Number( 42 );
if (a == 2 && a == 3) {
console.log( "Yep, this happened." );
}
============================
"0" == null; // false
"0" == undefined; // false
"0" == false; // true -- UH OH!
"0" == NaN; // false
"0" == 0; // true
"0" == ""; // false
false == null; // false
false == undefined; // false
false == NaN; // false
false == 0; // true -- UH OH!
false == ""; // true -- UH OH!
false == []; // true -- UH OH!
false == {}; // false
"" == null; // false
"" == undefined; // false
"" == NaN; // false
"" == 0; // true -- UH OH!
"" == []; // true -- UH OH!
"" == {}; // false
0 == null; // false
0 == undefined; // false
0 == NaN; // false
0 == []; // true -- UH OH!
0 == {}; // false
[] == ![]; // true
2 == [2]; // true
"" == [null]; // true
0 == "\n"; // true
var a = null;
var b = Object( a ); // same as `Object()`
a == b; // false
var c = undefined;
var d = Object( c ); // same as `Object()`
c == d; // false
var e = NaN;
var f = Object( e ); // same as `new Number( e )`
e == f; // false
======================================================
ES5规范中的解释:
Comparing: objects to nonobjects
If an object/function/array is compared to a simple scalar primitive
(string, number, or boolean), the ES5 spec says in clauses
11.9.3.8-9:
1. If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
2. If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Comparing: nulls to undefineds
Another example of implicit coercion can be seen with == loose
equality between null and undefined values. Yet again quoting the
ES5 spec, clauses 11.9.3.2-3:
1. If x is null and y is undefined, return true.
2. If x is undefined and y is null, return true.
Comparing: anything to boolean
1. If Type(x) is Boolean, return the result of the comparison
ToNumber(x) == y.
2. If Type(y) is Boolean, return the result of the comparison x ==
ToNumber(y).
Comparing: strings to numbers
1. If Type(x) is Number and Type(y) is String, return the result of
the comparison x == ToNumber(y).
2. If Type(x) is String and Type(y) is Number, return the result of
the comparison ToNumber(x) == y.
==============================直观的比較图
http://dorey.github.io/JavaScript-Equality-Table/
相关文章推荐
- 你真得懂Javascript中的==等于运算符吗?
- Javascript 按位与运算符 (&)使用介绍
- javascript基础语法-运算符
- JavaScript算术运算符与表达式 赋值运算符(+,=)
- 由一道题目引出的JavaScript中运算符的优先级问题
- javascript基础之表达式和运算符
- JavaScript 无符号右移运算符
- JavaScript 入门·JavaScript 具有全范围的运算符
- JavaScript的数据类型,运算符及数据类型之间的转化
- JavaScript 运算符和表达式
- javascript操作符和运算符
- 编写可维护的JavaScript—语句和表达式&变量、函数和运算符
- JavaScript基础----05Javascript语法-运算符(2)
- 简略说明Javascript中的= =(等于)与= = =(全等于)区别
- javascript 表达式和运算符 (二)
- JavaScript学习笔记(8)——JavaScript语法之运算符
- javascript运算符的优先级
- JavaScript instanceof 运算符深入剖析
- JavaScript -- 相等运算符
- javascript学习笔记:new运算符