如果条件不在内(!!)

时间:2015-05-18 06:13:32

标签: javascript if-statement operators conditional

注意:它实际上是What is the difference between if(!!condition) and if(condition)

的副本

虽然我理解what the !! means (double not),但出于同样的原因,我使用它in the MDN documentation并不合理:

if (!!window.Worker) {
...
}

这种情况不完全相同吗?

if (window.Worker) {
...
}

对我来说,转换为boolean是没有意义的,因为只有if存在才会执行window.Worker。要说它TrueObject if()有条件(我认为)是相同的。

那么,为什么!!在这里使用?或者,为什么window.Workerif()内投放到布尔值?

1 个答案:

答案 0 :(得分:2)

是的,它完全一样。没什么可补充的。

它可能已经被用来强调window.Worker属性 - 期望是一个函数 - 被强制转换为布尔值来检测它的存在,而不是看起来像一个被遗忘的()调用。无论如何,it is now gone