比较运算 - 语法正确吗?

发布于 2024-10-29 07:06:40 字数 598 浏览 1 评论 0原文

来自 PHP,我理解在比较操作中使用 == 和 === 的区别,但是对于 JavaScript 来说,使用 == 或 === 可以接受吗?

我想验证我的 JavaScript 代码,并找到了 JSLint。在使用 JSLint 运行验证时,我看到了这些类型的消息。

一些示例:

var show = $('input[name=female]:checked').val() == "true";
  • 第 278 行第 70 行字符的问题:预期为 '!==',但看到的是 '!='。

第 283 行第 38 行的问题

var show = $('input[name=living]:checked').val() != "false";
  • :预期为“!==”,但看到了“!=”。

所以我的问题是,使用 == 或 != 的当前语法是否有效/正确,或者我是否需要使用 === 和 !== ?

编辑:只是指出代码确实有效

Coming from PHP I understand the difference in using == verses === in a comparison operation, but with JavaScript is it acceptable to use == or ===?

I wanted to validate my JavaScript code and found JSLint. While running the validation with JSLint I see these types of messages.

Some examples:

var show = $('input[name=female]:checked').val() == "true";
  • Problem at line 278 character 70: Expected '!==' and instead saw '!='.

and

var show = $('input[name=living]:checked').val() != "false";
  • Problem at line 283 character 38: Expected '!==' and instead saw '!='.

So My question is, Would using the current syntax of == or != be valid/correct or do I need to use === and !== ?

EDIT: Just to point out the code does work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

大姐,你呐 2024-11-05 07:06:40

它在某种程度上是正确的,但 JSLint 希望进行严格比较以防止类型转换出现问题。

最好只在您真正需要/想要的地方使用严格比较和“正常”比较(因为您出于某种原因想要使用类型转换)。

来自 JSLint 的 文档

== 和 !=

==!= 运算符确实会输入
比较前强制。这很糟糕
因为它会导致 ' \t\r\n' == 0
true。这可以掩盖类型错误。

如果你只关心一个值是
,然后使用短
形式。而不是

<前><代码>(foo != 0)

就说

<前><代码>(foo)

而不是

<前><代码>(foo == 0)

<前><代码>(!foo)

始终使用 ===!==
运算符。

It is correct in a way but JSLint expects strict comparison to prevent problems with type conversion.

It is good practice to always use strict comparison and "normal" comparison only where you really need/want it (because you want to make use of type conversion for some reason).

From JSLint's documentation:

== and !=

The == and != operators do type
coercion before comparing. This is bad
because it causes ' \t\r\n' == 0 to
be true. This can mask type errors.

If you only care that a value is
truthy or falsy, then use the short
form. Instead of

(foo != 0)

just say

(foo)

and instead of

(foo == 0)

say

(!foo)

Always use the === and !==
operators.

余罪 2024-11-05 07:06:40

Crockford 和 JSLint 在这一点上有些教条,我认为过于规范。对于缺乏经验的 JavaScript 开发人员来说,始终使用 === 而不是 == 是一个合理的经验法则,但如果您了解基本规则,那么使用 就没有问题>==。特别是,如果保证两个操作数具有相同类型(例如在 typeof 比较中,如 typeof foo == "undefined"),则这两个运算符是指定遵循完全相同的步骤。

习惯性地使用 === 也有一个合理的观点,即无需考虑使用哪个运算符,但坦率地说,我更喜欢被迫考虑我所使用的两个操作数的所有可能值。比较一下,发现JSLint没有必要保姆。

Crockford and JSLint are somewhat dogmatic on this point, and I think over-prescriptive. Always using === rather than == is a reasonable rule of thumb for inexperienced JavaScript developers, but if you know the basic rules then there is no problem with using ==. In particular, if the two operands are guaranteed to be of the same type (such as in a typeof comparison like typeof foo == "undefined") then the two operators are specified to follow precisely the same steps.

There is also a reasonable point about habitually using === removing the need to think about which operator to use, but frankly I prefer to be forced to consider all the possible values of the two operands I'm comparing and find JSLint unnecessarily nannying.

海的爱人是光 2024-11-05 07:06:40

== 语法是有效的,但在某些情况下,它可能会导致大型项目中出现相当严重的错误。如果你没有充分的理由使用 ==,你应该默认使用 === 编写 JavaScript,因为它会检查你知道的类型。

无论哪种方式都有效,但如果你默认为===,你会在将来避免痛苦。

The == syntax is valid but in some cases it can lead to rather nasty bugs in large projects. If you don't have a good reason to use == you should by default write JavaScript with === because it checks for type as you know.

It works either way, but you will save yourself pain in the future if you default to ===.

念﹏祤嫣 2024-11-05 07:06:40

视情况而定。

=== 通常是首选,因为您不会有任何您可能没有考虑到的误报(在某些实现中它也可能稍微快一些,因为它与较小的潜在值相比,尽管我没有检查过,只是一个认为,速度差异可以忽略不计)。

== 通常更方便地覆盖许多/所有情况(如果我没记错的话,jQuery 不会通过 JSLint,因为在某些地方,代码有意使用 == 来覆盖多种情况,而不是单独测试每个情况)。

Depends on the situation.

=== is often preferred because you won't have any false-positives that you might not have considered (it might also be slightly quicker in some implementations because it compares to less potential values, although I haven't checked that, just a thought, and the speed difference would be negligible).

== is often more convenient to cover many/all cases (if I'm not mistaken, jQuery doesn't pass JSLint because in some places the code intentionally uses == to cover a multitude of cases rather than testing each one individually).

梦回旧景 2024-11-05 07:06:40

你可以只做 == 和 != ,我认为这样做不会有任何问题。

you can do just == and !=, I don't think there would be any problems with that.

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