Javascript 双感叹号是什么!!运营商是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7452720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the double exclamation !! operator mean?
提问by stirfries
Possible Duplicate:
What is the !! operator in JavaScript?
What does !! (double exclamation point) mean?
I am going through some custom JavaScript code at my workplace and I am not able to understand the following construct.
我正在我的工作场所浏览一些自定义 JavaScript 代码,但我无法理解以下结构。
var myThemeKey = (!!$('row') && $('row').hasClassName('green-theme')) ? 'green' : 'white';
I understand everything on the above line except !!
operator. I assume that it is a NOT
operator and NOT
of NOT
is the original value but why would someone do a NOT
of NOT
?
除了!!
操作符,我理解上面一行的所有内容。我假设它是一个NOT
运算符,而NOT
ofNOT
是原始值,但为什么有人会做 a NOT
of NOT
?
Can someone please help me understand what is happening on the above line of code?
有人可以帮我理解上面的代码行发生了什么吗?
回答by i_am_jorf
The !!
ensures the resulting type is a boolean (true or false).
在!!
确保得到的类型是布尔型(真或假)。
javascript:alert("foo")
--> foo
javascript:alert("foo")
--> foo
javascript:alert(!"foo")
--> false
javascript:alert(!"foo")
--> false
javascript:alert(!!"foo")
--> true
javascript:alert(!!"foo")
--> true
javascript:alert(!!null)
--> false
javascript:alert(!!null)
--> false
They do this to make sure $('row')
isn't null.
他们这样做是为了确保$('row')
不为空。
It's shorter to type than $('row') != null ? true : false
.
输入比$('row') != null ? true : false
.