当前位置: 首页 > 面试题库 >

!!〜(not not tilde/bang bang tilde)如何改变“contains/included” Array方法调用的结果?

卢鸿彩
2023-03-14
问题内容

如果您在此处的jQueryinArray页面上阅读了注释,则会有一个有趣的声明:

!!~jQuery.inArray(elm, arr)

现在,我相信双感叹号会将结果转换为typeboolean,其值为true。我不明白的是,~在所有这些操作中,tilde()运算符的用途是什么?

var arr = ["one", "two", "three"];
if (jQuery.inArray("one", arr) > -1) { alert("Found"); }

重构if语句:

if (!!~jQuery.inArray("one", arr)) { alert("Found"); }

分解:

jQuery.inArray("one", arr)     // 0
~jQuery.inArray("one", arr)    // -1 (why?)
!~jQuery.inArray("one", arr)   // false
!!~jQuery.inArray("one", arr)  // true

我还注意到,如果将波浪号放在前面,结果是-2

~!!~jQuery.inArray("one", arr) // -2

我不明白这里的波浪符号的目的。有人可以解释一下还是将我指向资源?


问题答案:

波浪号运算符实际上根本不是jQuery的一部分-在JavaScript本身中是按位NOT运算符。

参见 The Great Mystery of the Tilde(~).

您在实验中得到了奇怪的数字,因为您正在对整数执行按位html" target="_blank">逻辑运算(就我所知,整数可能存储为二进制补码或类似的数字…)

_二进制补码_说明了如何用二进制表示数字。我想我是对的。



 类似资料:
  • Contains Duplicate 描述 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return fal

  • 我需要了解如何将方法返回到方法中,以打印出计算机的随机选择。 打印语句之后的最后一个方法不完整;我只是被这部分卡住了。

  • Contains Duplicate III 描述 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the dif

  • Contains Duplicate II 描述 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and

  • 问题内容: 我执行了以下代码,发现输出为。 我想知道它的行为以及为什么输出是。 问题答案: 您还需要覆盖方法。这两种方法都用于适当的功能,因此必须被覆盖在 用户定义的类 ,如果你制作的作为,其他的类习惯并没有两个不同的可被视为与它们的将永远是不同的,并且肯定会在情况下总是返回。

  • 问题内容: 说我有Method1(void),Method2(void)… 有什么方法可以选择其中一个带有变量的方法吗? 问题答案: 使用反射: