使用双重否定 (!!) [英] Use of double negation (!!)

查看:29
本文介绍了使用双重否定 (!!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我遇到了一个看起来像这样的代码

Okay so I came across a code which looks like

@documents_names = sort {
         !!$deleted_documents_names{$a} == !!$deleted_documents_names{$b}
          ? uc($a) cmp uc($b)
          : !!$deleted_documents_names{$a}
          cmp !!$deleted_documents_names{$b}
         } @documents_names;

这是我第一次看到双重否定的使用.它有什么用?人们什么时候会使用它?

It's the first time I'm seeing the use of double negation. What's the use of it? When would a person use it?

推荐答案

将非布尔类型转换为布尔类型(dualvar(0,"") or 1).

It converts non-boolean types to boolean (dualvar(0,"") or 1).

这是执行此操作的快捷方式,而不是尝试显式强制转换(可能需要更多字符).! 运算符否定其参数的真实性.因此,使用了其中的两个.

It is a shortcut way of doing this, instead of trying to cast it explicitly (which may take more characters). The ! operator negates the truthness of its argument. Hence, two of them are used.

许多对象类型是真实的",而其他对象类型是虚假的".

Many object types are "truthy", and others are "falsey".

  • 唯一的错误值是 0undef"""0" 和一些重载对象.
  • 真值的示例是 1"asdf" 和所有其他值.
  • The only false values are 0, undef, "", "0" and some overloaded objects.
  • Examples of true values are 1, "asdf", and all other values.

这篇关于使用双重否定 (!!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆