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/

你真得懂Javascript中的==等于运算符吗?的更多相关文章

  1. 来一轮带注释的demo,彻底搞懂javascript中的replace函数

    javascript这门语言一直就像一位带着面纱的美女,总是看不清,摸不透,一直专注服务器端,也从来没有特别重视过,直到最近几年,javascript越来越重要,越来越通用.最近和前端走的比较近,借此 ...

  2. 浅析JavaScript中的typeof运算符

    对JavaScript中的typeof运算符进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助. 如果typeof的运算符是数字.字符串或者布尔值,它返回的结果就是"numb ...

  3. 你真的懂javascript中的 “this” 吗?

    一.前言: 我们知道 "this" 是javascript语言的一个关键字,在编写javascript代码的时候,经常会见到或者用到它. 但是,有一部分开发朋友,对 "t ...

  4. 彻底搞懂JavaScript中的继承

    你应该知道,JavaScript是一门基于原型链的语言,而我们今天的主题 -- "继承"就和"原型链"这一概念息息相关.甚至可以说,所谓的"原型链&q ...

  5. 一张图搞懂 Javascript 中的原型链、prototype、__proto__的关系 转载加自己的总结

    1. JavaScript内置对象 所谓的内置对象 指的是:JavaScript本身就自己有的对象 可以直接拿来就用.例如Array String 等等.JavaScript一共有12内置对象    ...

  6. 一文彻底搞懂JavaScript中的prototype

    prototype初步认识 在学习JavaScript中,遇到了prototype,经过一番了解,知道它是可以进行动态扩展的 function Func(){}; var func1 = new Fu ...

  7. JavaScript中涉及得运算符以及运算符的优先级

    在js中主要有三种运算符:算术运算符,逻辑与比较运算符,位运算符.在着三种运算符中,最常见的应该是算术与比较运算符,位运算符比较少见一些 *说到了运算符,就不得不说运算符的优先级.下面我来列一下这些运 ...

  8. JavaScript中的逗号运算符

    JavaScript逗号运算符  阅读本文的前提,明确表达式.短语.运算符.运算数这几个概念. 所谓表达式,就是一个JavaScript的“短语”,JavaScript解释器可以计算它,从而生成一个值 ...

  9. JavaScript学习系列8 - JavaScript中的关系运算符

    JavaScript中有8个关系运算符,分别是 ===, !===, ==, !=, <, <=, >, >= 1. 恒等运算符 (===) ===也叫做 严格相等运算符,它要 ...

随机推荐

  1. js003-4方向8方向函数

    1,求四方向或者8方向的周围的棋子. /** * pos 1-4, 1-8 4/8方向的周围 * @param {*} pos * @param {*} dir */ var _nearChess = ...

  2. nginx 实现跨域

    nginx 添加头部跨域. location / { add_header 'Access-Control-Allow-Origin' '*'; //允许的域 add_header 'Access-C ...

  3. expr---计算工具

    expr命令是一款表达式计算工具,使用它完成表达式的求值操作. expr的常用运算符: 加法运算:+ 减法运算:- 乘法运算:\* 除法运算:/ 求摸(取余)运算:% 语法 expr(选项)(参数) ...

  4. Xcode Git 客户端 + GitBucket 服务器 使用整合归纳

    1.使用前说明: 不知道XCode,Git的请自行度娘,不知道GitBucket的,可参考文章:git 私服搭建-gitbucket 初试牛刀 2.创建Git步骤 1>在gitbucket创建账 ...

  5. Android 经常使用的adb命令

    1.安装APK(假设加 -r 參数,保留已设定数据.又一次安装filename.apk) adb install xxx.apk adb install -r xxx.apk 2.卸载APK(假设加 ...

  6. Intersection between a 2d line and a conic in OpenCASCADE

    Intersection between a 2d line and a conic in OpenCASCADE eryar@163.com Abstract. OpenCASCADE provid ...

  7. Java io流的学习

    近期几天细致学了Java的io流.本来是打算看视频通过视频来学习的.但是后来发现事实上视频看不怎么懂也感觉不是非常easy上手,所以就通过百度和api文档学习了Java的io流 io流能够有两个分类, ...

  8. IIS文件上传大小修改配置说明

    原因:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值(IIS 7 默认文件上传大小时30M). 解决:IIS7更改asp.net文件上传大小限制 步骤如下: 1.    修改I ...

  9. 基于Linux平台的Openvas配置使用视频教学

    常见的漏洞扫描工具有Openvas.LSAT.Nessus.X-scan.ShadowSecurityScanner和流光等,openvas是一款最全面的开源漏洞扫描工具,由于openvas安装比较困 ...

  10. BZOJ5332: [Sdoi2018]旧试题(莫比乌斯反演)

    时光匆匆,转眼间又是一年寒暑…… 这是小 Q 同学第二次参加省队选拔赛. 今年,小 Q 痛定思痛,不再冒险偷取试题,而是通过练习旧 试题提升个人实力.可是旧试题太多了,小 Q 没日没夜地做题,却看不到 ...