你真得懂Javascript中的==等于运算符吗?
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/
最新文章
- Hololens 硬件细节 Hardware Detail
- UIApplication详解
- Adding a WebPart to a SharePoint 2013 Master Page 分类: Sharepoint 2015-07-08 01:03 7人阅读 评论(0) 收藏
- XCode一直显示";scanning for working copies";的解决办法
- Oracle Dataguard之failover
- php函数mt_rand和rand 速度测试
- 【转】beancopy的替代方案
- [string]Codeforces158C Cd and pwd commands
- nodejs上传图片模块做法;
- TCP/IP 和HTTP 协议基础知识
- FZU 2112 Tickets
- Swift 2.2 多态和强制转换
- Android学习总结——开篇
- 解决其他浏览器没有propertychange事件
- SSM-MyBatis-17:Mybatis中一级缓存(主要是一级缓存存在性的证明,增删改对一级缓存会造成什么影响)
- WPF 毛笔字
- 31.C++-虚函数之构造函数与析构函数分析
- IOS初级:story board的跳转
- 《C#本质论(第4版)》
- iis配置asp.net常见问题解决方案