我看到一些代码似乎使用了我不认识的运算符,以两个感叹号的形式,像这样:!!。有人能告诉我这个接线员做什么吗?

我看到这件事的背景是,

this.vertical = vertical !== undefined ? !!vertical : this.vertical;

当前回答

这是我找到的最简单的答案:它的工作方式相当于布尔值。

其他回答

这是一次双重手术。第一个!将值转换为布尔值并反转其逻辑值。第二个!反转逻辑值。

这只是逻辑NOT运算符,两次。它用于将某些内容转换为布尔值,例如:

true === !!10

false === !!0

这个构造是将任何JavaScript表达式转换为它的布尔等价物。

例如:!!“他击落了我”==真的!!0===假。

只是为了检查是否存在

if(!!isComplianceOnHold){
//write code here is not undefined
//if isComplianceOnHold is undefined or null will not enter in net is false
// if isComplianceOnHold is not null or even boolean net result is true and enter inside if block
}

任何值未定义或为null的对象,包括值为false的布尔对象,在传递给条件语句时其计算结果为true

这里有很多很棒的答案,但如果你读到这里,这帮助我“找到答案”。在Chrome(等)中打开控制台,然后开始键入:

!(!(1))
!(!(0))
!(!('truthy')) 
!(!(null))
!(!(''))
!(!(undefined))
!(!(new Object())
!(!({}))
woo = 'hoo'
!(!(woo))
...etc., etc., until the light goes on ;)

当然,这些都和仅仅打字一样!!但添加的括号可能有助于使其更易于理解。