回答

收藏

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

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

我看到一些代码似乎使用了我不知道的操作符,以两个感叹号的形式出现,如下所示:!. 有人能告诉我操作员在做什么吗?5 w" r. `3 C6 {4 f: R
我看到的背景是,
' V* X  U) N! e  O( ethis.vertical = vertical !== undefined ? !!vertical : this.vertical;               
0 G0 p2 [5 D/ a6 G" m1 a    解决方案:                                                               
+ h# Z4 ]- M  `) s2 M7 ?6 L! ~: g                                                                转换Object为boolean. 如果是falsey(例如0,null,undefined等等),这将是false,否则,true。
. i; o/ z1 ^  b! M# Q!oObject  // inverted boolean!!oObject // non inverted boolean so true boolean representation所以!!不是运算符,只是!运算符两次。
6 S' c: @; n, c1 t* z0 T  V现实世界示例测试 IE 版本”:2 g9 @( y! C, f& w' T( w
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);  console.log(isIE8); // returns true or false 如果你?2 g, [1 I8 \% W4 S  y  a3 T# }3 C
console.log(navigator.userAgent.match(/MSIE 8. / / / returns either an Array or null  但是如果是你的话- p8 Z1 a6 g; p# b6 m/ x4 c
console.log(!!navigator.userAgent.match(/MSIE 8. / / / returns either true or false
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则