波浪号在表达式之前有什么作用? [英] What does a tilde do when it precedes an expression?

查看:21
本文介绍了波浪号在表达式之前有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var attr = ~'input,textarea'.indexOf( target.tagName.toLowerCase() )?'价值':'内部HTML'

我在一个答案中看到过,以前从未见过.

什么意思?

解决方案

~ 是一个 按位运算符 翻转其操作数中的所有位.

例如,如果您的号码是 1,则其 IEEE 754 浮点数(JavaScript 如何处理数字)将是...

0011 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

所以 ~ 将其操作数转换为 32 位整数(JavaScript 中的按位运算符会这样做)...

0000 0000 0000 0000 0000 0000 0000 0001

如果是负数,则以 2 的补码形式存储:反转所有位并加 1.

...然后翻转所有位...

1111 1111 1111 1111 1111 1111 1111 1110

<块引用>

那它有什么用呢?什么时候可以使用它?

它有很多用途.如果你在写低级的东西,它很方便.如果您分析了您的应用程序并发现了瓶颈,则可以通过使用按位技巧(作为一个可能工具放在更大的包中)提高其性能.

indexOf()found 返回值转换为 truthy 也是一个(通常)不清楚的技巧>(同时使 not foundfalsy)并且人们经常使用它来将数字截断为 32 位(并通过加倍去掉小数位,有效地与正数的 Math.floor() 相同).

我说不清楚是因为它的用途并不是很明显.通常,您希望您的代码能够与阅读它的其他人清楚地交流.虽然使用 ~ 可能看起来很酷,但它通常太聪明了.:)

现在 JavaScript 具有Array.prototype.includes()String.prototype.includes().这些返回一个布尔值.如果您的目标平台支持它,您应该更喜欢使用它来测试字符串或数组中的值是否存在.

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.

这篇关于波浪号在表达式之前有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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