.jpg)
我看到一些代码似乎使用了我不认识的运算符,以两个感叹号的形式出现,如下所示:。有人可以告诉我这个操作员是做什么的吗?!!
我看到这个的背景是,
this.vertical = vertical !== undefined ? !!vertical : this.vertical;

网友回答:
!!expr(两个运算符后跟一个表达式)返回一个布尔值(或),具体取决于表达式的真实性。当用于非布尔类型时,它更有意义。考虑这些示例,尤其是第三个及以后的示例:!truefalse
!!false === false
!!true === true
!!0 === false
!!parseInt("foo") === false // NaN is falsy
!!1 === true
!!-1 === true // -1 is truthy
!!(1/0) === true // Infinity is truthy
!!"" === false // empty string is falsy
!!"foo" === true // non-empty string is truthy
!!"false" === true // ...even if it contains a falsy value
!!window.foo === false // undefined value is falsy
!!undefined === false // undefined primitive is falsy
!!null === false // null is falsy
!!{} === true // an (empty) object is truthy
!![] === true // an (empty) array is truthy; PHP programmers beware!

网友回答:
它将转换为 .如果它是假的(例如,、、等),则为,否则,.Objectboolean0nullundefinedfalsetrue
!object // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representation
所以不是操作员;只是操作员两次。!!!
这样做可能更简单:
Boolean(object) // Boolean
真实示例“测试 IE 版本”:
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or false
如果您⇒
console.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or null
但如果你⇒
console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or false

网友回答:
这是一种非常晦涩难懂的类型转换方式。
!表示不。所以是 ,是 。 是 ,并且是 。!truefalse!falsetrue!0true!1false
因此,您将值转换为布尔值,反转它,然后再次反转它。
// Maximum Obscurity:
val.enabled = !!userId;
// Partial Obscurity:
val.enabled = (userId != 0) ? true : false;
// And finally, much easier to understand:
val.enabled = (userId != 0);
// Or just
val.enabled = Boolean(userId);
注意:当涉及到某些边缘情况(例如,当是)时,后两个表达式并不完全等同于第一个表达式,因为运算符的工作方式以及哪些值被认为是真实的。userId[]!=
模板简介:该模板名称为【以两个感叹号的形式出的运算符操作作用是什么?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【JavaScript】栏目查找您需要的精美模板。