We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
看《你所不知道的JavaScript》一书,发现对相等运算符的描述推翻了许多人的固有影响,如果熟悉其机制,可以方便我们快速开发
function foo(s) { s = s == null ? true: s; // ... };
上面这段代码我们估计经常使用,其实就是使用了隐式转换,下面就来聊聊相等运算符
42 == "42"; // 等于 42 == Number("42");
"42" == true; // 等于 "42" == Number(true); "42" == 1; Number("42") == 1;
// 全部false null == false; null == true; null == ""; null == 0; undefined == true; undefined == false; undefined == ""; undefined == 0;
valueOf
toStirng
[42] == 42; // 等于 "42" == 42; Number("42") == 42; "abc" == new String("abc"); // 等于 "abc" == "abc";
根据对象转化规则,我们自定义返回的值,比如:
var a = []; a.valueOf = function() { return 42; }; a.toString = function() { return "123"; }; a == 42; //true
上面简单介绍了一下规则,再来看一下隐形的问题
"0" == false; // true false == 0; // true false == ""; // true false == []; // true "" == []; // true 0 == []; // true "" == 0; // true
[null] == ""; // 转化为 "" == "";
// a可能为true执行下去么 if (a == 2 && a == 3) { // 执行里面的代码 }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
上面这段代码我们估计经常使用,其实就是使用了隐式转换,下面就来聊聊相等运算符
规则
对象与基本类型比较,会将对象转化为基本类型之后再进行比较;
转化规则很简单,默认调用
valueOf
,如果返回不是基本类型继续调用toStirng
,如果返回还不是基本类型会报错。根据对象转化规则,我们自定义返回的值,比如:
实战
上面简单介绍了一下规则,再来看一下隐形的问题
这里会返回 true 原因很简单,根据上述规则其实也就是"" == 0比较
这里会返回 true,因为空格和一些其他制表符会被忽略掉。 再来看一些常见的陷阱
这里为true,可能你在想[null]返回的不就是字符串 null 么,不过很遗憾,null 和 undefined,在数组中转化为字符串为"",这是 JavaScript 所规定的。
最后
The text was updated successfully, but these errors were encountered: