看到源码中有一段JS代码不太懂,如下:
里面这个 "!~" 符号看到后有点儿方啊O__O "…,毛线意思?
【查资料,解释如下】:
indexOf
returns -1 when an element cannot be found in an array. Therefore, the if
statement is checking if name
could not be found in names
. !~-1 ==> true
indeOf 数组方法在应用时,如果元素不存在于数组内则返回 -1 。因此,申明一个元素不在数组内的,就可以使用语法 !~-1 //true
The tilde (~
) operator (bitwise NOT) yields the inverted value (a.k.a. one’s complement) of a.
波浪线 ~ 操作符会按位取反
~-1 === 0
. Note that 0 == false
and !0 === true
.
注意到 0 == false 、!0 === true
indexOf
returns -1 when an element cannot be found in an array.
当元素不存在数组内时,indexOf 返回 -1
Therefore, we can use !~-1 === true
to find out if indexOf
could not find name
in names
(i.e. returned -1).
因此,元素不存在数组内的话,就可以使用 !~-1 === true 来进行判断