What does a tilde do when it precedes an expression?
var attr = ~'input,textarea'.indexOf( target.tagName.toLowerCase() )
? 'value'
: 'innerHTML'
I saw it in an answer, and I've never seen it before.
What does it mean?
~
is a bitwise operator that flips all bits in its operand.
For example, if your number was 1
, its binary representation of the IEEE 754 float (how JavaScript treats numbers) would be...
0011 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
So ~
converts its operand to a 32 bit integer (bitwise operators in JavaScript do that)...
0000 0000 0000 0000 0000 0000 0000 0001
If it were a negative number, it'd be stored in 2's complement: invert all bits and add 1.
...and then flips all its bits...
1111 1111 1111 1111 1111 1111 1111 1110
So what is the use of it, then? When might one ever use it?
It has a quite a few uses. If you're writing low level stuff, it's handy. If you profiled your application and found a bottleneck, it could be made more performant by using bitwise tricks (as one possible tool in a much bigger bag).
It's also a (generally) unclear trick to turn indexOf()
's found return value into truthy (while making not found as falsy) and people often use it for its side effect of truncating numbers to 32 bits (and dropping its decimal place by doubling it, effectively the same as Math.floor()
for positive numbers).
I say unclear because it's not immediately obvious what it is being used for. Generally, you want your code to communicate clearly to other people reading it. While using ~
may look cool, it's generally too clever for its own good. :)
It's also less relevant now that JavaScript has Array.prototype.includes()
and String.prototype.includes()
. These return a boolean value. If your target platform(s) support it, you should prefer this for testing for the existence of a value in a string or array.
Using it before an indexOf()
expression effectively gives you a truthy/falsy result instead of the numeric index that's directly returned.
If the return value is -1
, then ~-1
is 0
because -1
is a string of all 1 bits. Any value greater than or equal to zero will give a non-zero result. Thus,
if (~someString.indexOf(something)) {
}
will cause the if
code to run when "something" is in "someString". If you try to use .indexOf()
as a boolean directly, then that won't work because sometimes it returns zero (when "something" is at the beginning of the string).
Of course, this works too:
if (someString.indexOf(something) >= 0) {
}
and it's considerably less mysterious.
Sometimes you'll also see this:
var i = ~~something;
Using the ~
operator twice like that is a quick way to convert a string to a 32-bit integer. The first ~
does the conversion, and the second ~
flips the bits back. Of course if the operator is applied to something that's cannot be converted to a number, you get NaN
as a result. (edit — actually it's the second ~
that is applied first, but you get the idea.)
~indexOf(item)
经常出现,这里的答案很好,但也许有些人只需要知道如何使用它并“跳过”理论:
if (~list.indexOf(item)) {
// item in list
} else {
// item *not* in list
}
链接地址: http://www.djcxy.com/p/2096.html
上一篇: 如何在JavaScript中将字符串转换为布尔值?
下一篇: 在表达式之前,代字号在做什么?