JS 双感叹号——有什么好的理由使用它吗?
我和一位同事就这个话题争论了大约一周。我非常喜欢速记代码,尽可能使用三元组等。最近,他一直因为我使用双重感叹号而挑剔我。经过多次测试后,我开始同意他的观点......在我的代码中使用双重感叹号可能并不明智。考虑一下:
var myvar = "Hello";
return (!!myvar ? "Var is set" : "Var is not set");
上面的示例按预期工作。但是,如果我们检查可能返回未定义的变量,则会收到错误,尤其是在 IE7 中。然而,如果我们在控制台中运行它,我们会得到预期的结果:
if(randomvar) alert('Works');
使用这种方法,如果变量未定义,它会默默地失败。这让我对双重感叹号的使用产生疑问。是否有某种情况实际上使该操作员受益?
I've been debating this topic with a co-worker for about a week. I'm very much a fan of shorthand code, using ternaries, etc., wherever I can. Lately, he's been picking on me about my use of double exclamations. After running numerous tests, I'm beginning to agree with him... double exclamations may not be wise to use in my code. Consider this:
var myvar = "Hello";
return (!!myvar ? "Var is set" : "Var is not set");
The above example works as expected. However, if we are checking against a variable that may return undefined, we get an error, especially in IE7. We get our expected result, however, if we run this in our console:
if(randomvar) alert('Works');
Using this approach, if the variable is undefined, it fails silently. This makes me question the use of double exclamations altogether. Is there a situation that actually makes this operator beneficial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 javascript 中
!!
有一个有效的用途。它是一个表达式,它将接受一个值并转换为布尔值true
或false
。它本质上将当前状态强制转换为布尔值。都有好处,
==
)。并不能阻止所有这些情况,但将其强制为布尔值会删除一组场景。There is a valid use for
!!
in javascript. It's an expression which will take a value and convert to either the booleantrue
orfalse
. It essentially coerces the current state into a boolean.This is good for both
==
). Doesn't prevent them all but forcing it down to a bool removes a set of scenarios.!!x
将x
强制转换为布尔值,具有属性x == !!x
和typeof !!x === “布尔值”。 (如果您重写
valueOf
,一般情况下可能不是这样,但我现在不想考虑它。)!!x
coercesx
to a boolean, with the propertiesx == !!x
andtypeof !!x === "boolean"
. (Might not be true in general if you overridevalueOf
, but I don't want to think about it at the moment.)所以,我理解
typeof !!x === "boolean"
及其背后的逻辑(!x
转换为反转布尔值,!!x 反转,等等),但我想知道它本质上是否有问题?如果你转换一个未定义的变量,你最终会遇到脚本错误。我希望如果
typeof !!x === "undefined"
则!!x == false
,但事实并非如此。看来这将是更安全的选择:有人可以提供使用
!!x
更合适的场景吗?目前,我没有看到使用它的好处。抓挠我的头...
So, I understand that
typeof !!x === "boolean"
and the logic behind it (!x
casts as inverted boolean,!!x
inverts back, etc), but I'm wondering if it's problematic by nature? If you cast an undefined variable, you end up with a script error on your hands. I would expect that!!x == false
iftypeof !!x === "undefined"
, but that is not the case. It seems that this would be the safer choice:Can someone provide a scenario where using
!!x
is more appropriate? At the moment, I am not seeing a benefit to using it.Scratching my head...
我在 IE 中测试 -
!!undefined
返回 false。我相信这是正确的行为。至于
!!
在哪里(或可能)有用。下面是一个示例:
假设您有一个接受参数的函数:
function test(param)
{
if (参数 === true)
{
警报('确定');
}
}
调用:
test(1)
不会弹出警报窗口 - 尽管人们普遍认为 1 是true
或true< /code>-ish 值。是的,有人可能会争辩说,传递当前数据类型取决于团队中的协议和每个开发人员的责任 - 但 JavaScript 中正确的数据类型是什么?特别是当您没有编译器来验证哪个是哪个时。
因此,在上面的情况下,我会使用
if (!!param)
而不是if (param === true)
I tested in IE -
!!undefined
returns false. Which I believe a correct behavior.As for where
!!
is (or might be) useful.Here's an example:
Suppose you have a function that accepts a parameter:
function test(param)
{
if (param === true)
{
alert('OK');
}
}
Calling with:
test(1)
will not popup the alert window - although it is generally accepted that 1 is atrue
or atrue
-ish value. Yes someone could argue it is up to the agreement in the team and each of the developer responsibility to pass the currect data type - but what is the correct data type in JavaScript ? Especially when you don't have a compiler to verify which is which.Thus in the situation above, I'd use
if (!!param)
instead ofif (param === true)