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. jQuery和JavaScript的点击事件区别

    // $("#indexPage").click(); // 触发了a标签的点击事件,但是没有触发页面跳转 document.getElementById("indexP ...

  2. 【Henu ACM Round#16 C】Graph and String

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 根据题意:先明确以下规则: 1.如果两个点之间没有边,那么这两个点只能是a或c,且不能相同 2.如果两个点之间有边,那么他们之间的差 ...

  3. EditPlus 使用技巧以及快捷键

    一边阅读,一边动手吧! 为了达到更好的效果,请你先下载我打包的这个 EditPlus压缩包文件(压缩包文件为绿色的EditPlus2.31英文版,含自动完成文件,高亮语法文件和剪切板代码片断文件,这些 ...

  4. QTP校验数据库中数据后台项目

    数据校验功能后台主要包含两个类:QTPCommonServlet.java和QTPCommonDao.java 当中QTPCommonServlet.java为: package com.runqia ...

  5. codeforces 543 C Remembering Strings

    题意:若一个字符串集合里的每一个字符串都至少有一个字符满足在i位上,仅仅有它有,那么这个就是合法的,给出全部串的每一个字符修改的花费,求变成合法的最小代价. 做法:dp[i][j].前i个串的状态为j ...

  6. 改动UINavigationBar (导航栏)上NavigationBarItem 的字体大小和颜色的用法

    //创建一个左边button UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"<" ...

  7. 第一天,Mysql安装,DDL(数据库定义语言),DBA,DML(数据库操纵语言),导入外面的sql文件

    把“D:\mysql-5.6.22-winx64\bin”添加到系统环境变量path中了,然后在任意目录可访问mysql等命令,这样如登录等操作就不需要进入MySQL安装目录才好执行! MySQL下载 ...

  8. Mvc异步

    <h3>MVC 自带的yibu请求</h3> <%-- 1.要执行的方法,2.控制器,3.Ajax操作--%> <%using (Ajax.BeginForm ...

  9. SQL 金额添加千分位

    SELECT CONVERT(NVARCHAR(50),CAST(1000000 AS MONEY),1) SELECT CONVERT(NVARCHAR,CAST(1343432432434.8 A ...

  10. id---显示用户ID

    d命令   id命令可以显示真实有效的用户ID(UID)和组ID(GID).UID 是对一个用户的单一身份标识.组ID(GID)则对应多个UID 语法 id [-gGnru][--help][--ve ...