javascript 无语的==
今天面试不小心掉进坑了,大公司特别喜欢考javascript,而且专门挑很tricky的case。
javascipt的==简直就是黑魔法,以前偷懒总是用,感觉也没有问题,可是准备面试就需要有寻根问底的精神。
原题问[]==false; ![]==false console输出什么。结果是都是true
Since the left and right sides of the equality are two different types, JavaScript can't compare them directly. Hence, under the hood, JavaScript will convert them to compare. First, the right side of the equality will be cooereced to a number and number of true would be 1.
After that, JavaScript implementation will try to convert [] by usingtoPrimitive (of JavaScript implementation). Since [].valueOf is not primitive, it will use toString and will get ""
Now you are comparing "" == 1 and still, the left and right are not the same type. Hence, the left side will be converted again to a number and empty string will be 0.
Finally, they are of same type. You are comparing 0 === 1 which will be false.
一般做逻辑判断应该是转换为bool类型,javascript最终却是转换为数字来比较。
1.一个数字与一个字符串,字符串转换成数字之后,进行比较。
2. true转换为1,false转换为0进行比较。// 1==true返回true;2==true返回false;
3. 数组会被转换为原始类型之后进行比较(先valueof,不行的话再toString)。var a = [1,2], a==true这里的a会被转换为"1,2"又因为true被转成了数字,所以这里的a最终被转成Number(a),也就是NaN。
结合上面3点得出a==true最终变成 NaN==1 ,所以返回false。[1]==true;//true;[2]==true;//false!!
参见ecma262:http://www.ecma-international.org/ecma-262/6.0/index.html#sec-abstract-equality-comparison
7.2.12Abstract Equality Comparison
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- ReturnIfAbrupt(x).
- ReturnIfAbrupt(y).
- If Type(x) is the same as Type(y), then
- Return the result of performing Strict Equality Comparison x === y.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y). - If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y. - If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String, Number, or Symbol and Type(y) is Object, then
return the result of the comparison x == ToPrimitive(y). - If Type(x) is Object and Type(y) is either String, Number, or Symbol, then
return the result of the comparison ToPrimitive(x) == y. - Return false.
javascript 无语的==的更多相关文章
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...
- HTML 学习笔记 JavaScript(创建对象)
原博地址:http://www.cnblogs.com/dolphinX/p/3288118.html JavaScript 有Date Array String等这样的内置对象,功能强大实用简单,但 ...
- Javascript:一个屌丝的逆袭
HTML负责结构, CSS负责展示, 而我(加上AJAX, JSON) 负责逻辑.于是前端编程三剑客形成了. http://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExN ...
- JavaScript和JQuery好书推荐
其实无论你是php/python/java还是c/c++,只会自己那点知识是无法独立完成一个站点的建设的! 如果你因自己能力不足拒绝过几次亲友的建站请求,或者因合作中不了解前端是什么东西而失去过几次创 ...
- JavaScript和Java之间的关系
今天来简单而又详细地说说JavaScript和Java的关系. 开门见山总结性一句话,它们之间的关系 = 雷锋和雷峰塔之间的关系,换句话说:它们之间没什么关系. 但往往有不少初学者甚至中级者认为它们之 ...
- javaScript怪癖分析
最近了解到javascript中有些编程怪癖现象,很有意思,有必要总结一下: 1.未知变量名创建全局变量 在我们平常的编写javascript程序的时候,有的人写法不是很正规,在定义变量的时候 直接定 ...
- (转)javascript中的对象查找
本文转自:http://otakustay.com/object-lookup-in-javascript/ ---很棒的一篇文章,作者的其他文章还暂时没读,但相信作者是一个谦虚 谨慎的好工程师 近 ...
- javascript动态添加本地文件列表信息
工作需要做了一个动态添加列表页面的小demo.用到了杂七杂八的javascript小知识. 而且并没有涉及到工作中的具体情境.有些通用,所以暂且罗列到这里.以后需要的时候可以直接拿来用. 看源码总是让 ...
- Javascript与Flash通信全解析
原文:https://www.imququ.com/post/39.html Flash已经提供了ExternalInterface接口与JavaScript通信,ExternalInterface有 ...
随机推荐
- 深入理解java虚拟机【类加载机制】
Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程. 在加载阶段,java虚拟机需要完成以下 ...
- ubuntu 12.04 支持中文----完胜版
原文地址 http://pobeta.com/ubuntu-sublime.html, /* sublime-imfix.c Use LD_PRELOAD to interpose some func ...
- Twitter API升级至1.1
Twitter API 1.1是至今最大的一次升级,从3月份提出,到6月11日1.0版本已经全面停止调用.关于1.1版本升级特性可访问: https://dev.twitter.com/docs/ap ...
- 设置jenkins代理
http://stackoverflow.com/documentation/jenkins/919/introduction-to-jenkins Natively, Jenkins runs on ...
- 深入理解c++构造函数, 复制构造函数和赋值函数重载(operator=)
注 以下代码编译及运行环境均为 Xcode 6.4, LLVM 6.1 with GNU++11 support, Mac OS X 10.10.2 调用时机 看例子 // // main.cpp / ...
- Oracle dmp文件导入(还原)到不同的表空间和不同的用户下
------------------------------------- 从生产环境拷贝一个dmp备份文件,在另外一台电脑上搭建测试环境,用imp命令导入dmp文件时提示如下错误: 问题描述: IM ...
- SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE
1.创建表 Staff CREATE TABLE [dbo].[Staff]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Money] [int] NULL, [Cr ...
- 语音直播是否真能让国内网红向“Creator”转变?
2016年,“直播”.“网红”成为了互联网领域最热门的关键词,一时间整个国内市场涌现出了数百家直播平台,而一些视频网站.新闻客户端.社交平台.电商平台等也纷纷推出直播功能.不仅仅只是创业者们像发了疯似 ...
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
本周作业的主要内容就是采用gcc嵌入汇编的方式调用system call. 系统调用其实就是操作系统提供的服务.我们平时编写的程序,如果仅仅是数值计算,那么所有的过程都是在用户态完成的,但是我们想将变 ...
- Struts2入门1 Struts2基础知识
Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...