Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - 双感叹号? [重复](Double exclamation points? [duplicate])

Possible Duplicate:(可能重复:)

So I was debuging some code and ran across this:(所以我正在调试一些代码并遇到这个:)

var foo.bar = 0; // this is actually passed from another function, adding it for context function(foo) { var someVar = !!foo.bar; if (foo.bar) { // ..stuff happens } else { // .. something else happens } } Okay my questions is what is the point of !!(好的,我的问题是什么意思!!) ?(?) All that is doing is making the 0 === false .(所有这一切都是使0 === false 。) Is there any benefit to using that compared to boolean(foo.bar) ?(与boolean(foo.bar)相比,使用它有什么好处?) foo.bar can be evaluated in an if as is because 0 === false already, so why go through the conversion?(foo.bar可以在if as as中进行评估,因为0 === false ,所以为什么要进行转换?) (someVar is not reused anywhere else)((someVar不会在其他任何地方重复使用))   ask by jpalladino84 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This converts a value to a boolean and ensures a boolean type .(这会将值转换为布尔值并确保布尔类型 。)

"foo" // Evaluates to "foo". !"foo" // Evaluates to false. !!"foo" // Evaluates to true. If foo.bar is passed through, then it may not be 0 but some other falsy value.(如果foo.bar被传递,那么它可能不是0而是一些其他的虚假值。) See the following truth table:(请参阅以下真值表:) Truth Table for javascript(javascript的真值表) '' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true " " == 0 // true Source: Doug Crockford(资料来源:Doug Crockford) Javascript also gets really weird when it comes to NaN values.(当谈到NaN值时,Javascript也变得非常奇怪。) And this is the only case I can think of off the top of my head where !!(这是我能想到的最好的情况!) would behave differently to ===.(会对===表现不同。) NaN === NaN //false !!NaN === !!NaN //true // !!NaN is false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...