比较运算 - 语法正确吗?
来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它在某种程度上是正确的,但 JSLint 希望进行严格比较以防止类型转换出现问题。
最好只在您真正需要/想要的地方使用严格比较和“正常”比较(因为您出于某种原因想要使用类型转换)。
来自 JSLint 的 文档:
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:
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 atypeof
comparison liketypeof 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.== 语法是有效的,但在某些情况下,它可能会导致大型项目中出现相当严重的错误。如果你没有充分的理由使用 ==,你应该默认使用 === 编写 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 ===.
视情况而定。
=== 通常是首选,因为您不会有任何您可能没有考虑到的误报(在某些实现中它也可能稍微快一些,因为它与较小的潜在值相比,尽管我没有检查过,只是一个认为,速度差异可以忽略不计)。
== 通常更方便地覆盖许多/所有情况(如果我没记错的话,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).
你可以只做 == 和 != ,我认为这样做不会有任何问题。
you can do just == and !=, I don't think there would be any problems with that.