回答

收藏

JavaScript 中的!!是不是运算符?

技术问答 技术问答 399 人阅读 | 0 人回复 | 2023-09-11

我看到一些代码似乎使用了我不知道的操作符,以两个感叹号的形式出现,如下所示:!. 有人能告诉我操作员在做什么吗?1 ]1 P) c3 I& f' R' b
我看到的背景是,- K5 Z" T! ~8 [
this.vertical = vertical !== undefined ? !!vertical : this.vertical;               
( Q8 r" Y) t! ]5 V- t. k9 m: k, R    解决方案:                                                               
& Z5 u! Y" b. D( t                                                                转换Object为boolean. 如果是falsey(例如0,null,undefined等等),这将是false,否则,true。% r4 v( t" j" U8 h- C
!oObject  // inverted boolean!!oObject // non inverted boolean so true boolean representation所以!!不是运算符,只是!运算符两次。5 ^! w6 v$ e! S4 L" y3 z& ]
现实世界示例测试 IE 版本”:4 s' z. t1 d0 M% e! A$ e& c
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);  console.log(isIE8); // returns true or false 如果你?( u/ q9 V: I9 a  P/ q! h, q
console.log(navigator.userAgent.match(/MSIE 8. / / / returns either an Array or null  但是如果是你的话
. i1 M1 J, |8 y+ ?console.log(!!navigator.userAgent.match(/MSIE 8. / / / returns either true or false
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则