什么是顶级的JavaScript陷阱

什么是顶级的JavaScript陷阱

本文介绍了什么是顶级的JavaScript陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道,我知道,我知道我的这个故事,在我完全理解关闭之前,有几个问题,但JavaScript中的很多奇怪的行为并不是我想到的...



那么哪个陷阱应该你肯定会指出新秀?

解决方案

 ''=='0' // false 
0 ==''// true
0 =='0'// true
false =='false'// false
false =='0 '// true
false == undefined // false
false == null // false
null == undefined // true
\t\r\ n= = 0 // true

以及 null 未定义。如上表所列,比较 null & 未定义 == 返回true,但使用 === 它返回false。一旦您明白 undefined 与具有 null 值的变量有很大的不同,那么这个行为是有道理的,

I'm planing on giving an introduction talk on JavaScript and in the preparation process I wondered what the top pitfalls are that rookies fall into.

I know I've had a few gotchas before I fully understood closure, but much of the strange behavior in JavaScript is not something I think about any more...

So, which pitfalls should you definitely point out to the rookies?

解决方案

Boolean type conversion.

''        ==   '0'           //false
0         ==   ''            //true
0         ==   '0'           //true
false     ==   'false'       //false
false     ==   '0'           //true
false     ==   undefined     //false
false     ==   null          //false
null      ==   undefined     //true
" \t\r\n" ==   0             //true

As well as the difference between null and undefined. As listed in the table above, comparing null & undefined with == returns true, but with === it returns false. This behavior makes sense once you understand that undefined is very different from a variable having a null value, and something holding the value undefined is different from something being undefined.

这篇关于什么是顶级的JavaScript陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:08