Skip to content
New issue

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

一个让我掉到坑里的问题:null==0 吗? #35

Open
cssmagic opened this issue Feb 28, 2016 · 0 comments
Open

一个让我掉到坑里的问题:null==0 吗? #35

cssmagic opened this issue Feb 28, 2016 · 0 comments

Comments

@cssmagic
Copy link
Owner

cssmagic commented Feb 28, 2016

今天在处理某个 JavaScript 函数的过程中,犯了一个错误。我下意识地认为 null==0 是成立的。

我们知道 null===0 肯定是不成立的。但 null==0 呢?

JavaScript 是弱类型语言,有一套复杂的类型转换规则。记忆中 null 转换为布尔值应该是 false,而且 0 也是这样,于是就想当然地认为这个相等是成立的。

但是,相等运算符(==)并不是把两个值转换为布尔值之后再进行比较,它有自己的一套比较规则。我查阅了《JavaScript 权威指南》,结果证明我最初的想法是错误的。

这就是今天的一点收获。同时也要承认,作为一个 JavaScript 初学者,自己的基础还是有很多薄弱的地方!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant