JS 双感叹号——有什么好的理由使用它吗?

发布于 2024-12-05 12:38:48 字数 422 浏览 0 评论 0原文

我和一位同事就这个话题争论了大约一周。我非常喜欢速记代码,尽可能使用三元组等。最近,他一直因为我使用双重感叹号而挑剔我。经过多次测试后,我开始同意他的观点......在我的代码中使用双重感叹号可能并不明智。考虑一下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟火散人牵绊 2024-12-12 12:38:48

在 javascript 中 !! 有一个有效的用途。它是一个表达式,它将接受一个值并转换为布尔值 truefalse。它本质上将当前状态强制转换为布尔值。

都有好处,

  1. 这对于捕获值的真实性
  2. 释放原始对象以进行收集(应该是最终引用)
  3. 有助于防止以后错误地使用强制相等的对象(==)。并不能阻止所有这些情况,但将其强制为布尔值会删除一组场景。

There is a valid use for !! in javascript. It's an expression which will take a value and convert to either the boolean true or false. It essentially coerces the current state into a boolean.

This is good for both

  1. Capturing the truthiness of the value
  2. Freeing up the original object for collection (should that be the final reference)
  3. Helps prevent later incorrect usages of an object with coercing equality (==). Doesn't prevent them all but forcing it down to a bool removes a set of scenarios.
丶情人眼里出诗心の 2024-12-12 12:38:48

!!xx 强制转换为布尔值,具有属性 x == !!xtypeof !!x === “布尔值”。 (如果您重写 valueOf,一般情况下可能不是这样,但我现在不想考虑它。)

!!x coerces x to a boolean, with the properties x == !!x and typeof !!x === "boolean". (Might not be true in general if you override valueOf, but I don't want to think about it at the moment.)

浮生未歇 2024-12-12 12:38:48

所以,我理解 typeof !!x === "boolean" 及其背后的逻辑(!x 转换为反转布尔值,!!x 反转,等等),但我想知道它本质上是否有问题?如果你转换一个未定义的变量,你最终会遇到脚本错误。我希望如果 typeof !!x === "undefined"!!x == false ,但事实并非如此。看来这将是更安全的选择:

if((typeof x != "undefined") && (x != null)) { ... }

有人可以提供使用 !!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 if typeof !!x === "undefined", but that is not the case. It seems that this would be the safer choice:

if((typeof x != "undefined") && (x != null)) { ... }

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...

Oo萌小芽oO 2024-12-12 12:38:48

我在 IE 中测试 - !!undefined 返回 false。我相信这是正确的行为。

至于 !! 在哪里(或可能)有用。

下面是一个示例:

假设您有一个接受参数的函数:

function test(param)
{
if (参数 === true)
{
警报('确定');
}
}

调用:test(1) 不会弹出警报窗口 - 尽管人们普遍认为 1 是 truetrue< /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 a true or a true-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 of if (param === true)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文