我读 “发现流星” 的那一刻, 第七章是有代码:这是什么语法意味着----->“!!”在JavaScript中?
Posts.allow({
insert: function(userId, doc) {
// only allow posting if you are logged in
return !! userId; ///// <<==== what does "!!" means?
}
});
感谢
我读 “发现流星” 的那一刻, 第七章是有代码:这是什么语法意味着----->“!!”在JavaScript中?
Posts.allow({
insert: function(userId, doc) {
// only allow posting if you are logged in
return !! userId; ///// <<==== what does "!!" means?
}
});
感谢
精美总结了由汤姆·里特作为
// Maximum Obscurity:
val.enabled = !!userId;
// Partial Obscurity:
val.enabled = (userId != 0) ? true : false;
// And finally, much easier to understand:
val.enabled = (userId != 0);
因此在做强制转换为布尔值,然后做双重否定
哦,这是真的。它让我想起了Perl。大声笑 – CodeFarmer
!会将任何正值,真值或现有变量(如字符串和数组)转换为false,并将任何负值,undefined,null或false转换为true。 !!应用两次。
在此上下文中,如果变量userId存在并且不为空,空或假,它将返回true。
它只是喜欢你改变变量的类型为布尔
!! userId;
// same as
userId ? true:false;
的重复http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript – mavrosxristoforos