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

问题描述

好了,所以我碰到一个code,看起来像

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,) 1 )。

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

有这样做,而不是试图明确地投它(这可能需要多个字符)的快捷方式。在运营商否定了其参数的truthness。因此,他们两个被使用。

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.

许多对象类型是truthy,而其它的是falsey

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


  • 只有假值 0 民主基金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.

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

08-06 05:07