Javascript JSLint 预期为 '===',而是看到了 '=='
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3735939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSLint Expected '===' and instead saw '=='
提问by Metropolis
Recently I was running some of my code through JSLint when I came up with this error. The thing I think is funny about this error though is that it automatically assumes that all == should be ===.
最近,当我遇到这个错误时,我正在通过 JSLint 运行我的一些代码。我认为这个错误的有趣之处在于它自动假定所有 == 应该是 ===。
Does that really make any sense? I could see a lot of instances that you would not want to compare type, and I am worried that this could actually cause problems.
这真的有意义吗?我可以看到很多您不想比较类型的实例,我担心这实际上会导致问题。
The word "Expected" would imply that this should be done EVERY time.....That is what does not make sense to me.
“预期”这个词意味着每次都应该这样做......这对我来说没有意义。
回答by CMS
IMO, blindly using ===
, without trying to understandhow type conversionworks doesn't make much sense.
IMO,盲目使用===
,但不尝试了解如何类型转换工作没有太大的意义。
The primary fearabout the Equals operator ==
is that the comparison rules depending on the types compared can make the operator non-transitive, for example, if:
对 Equals 运算符的主要担心==
是取决于所比较类型的比较规则可能会使运算符不可传递,例如,如果:
A == B AND
B == C
Doesn't really guarantees that:
并不能真正保证:
A == C
For example:
例如:
'0' == 0; // true
0 == ''; // true
'0' == ''; // false
The Strict Equals operator ===
is not really necessary when you compare values of the same type, the most common example:
===
当您比较相同类型的值时,Strict Equals 运算符并不是真正必要的,最常见的例子是:
if (typeof foo == "function") {
//..
}
We compare the result of the typeof
operator, which is alwaysa string, with a stringliteral...
我们将typeof
运算符的结果(始终为string)与字符串文字进行比较...
Or when you know the type coercion rules, for example, check if something is null
or undefined
something:
或者当你知道类型强制规则时,例如,检查某事null
或undefined
某事:
if (foo == null) {
// foo is null or undefined
}
// Vs. the following non-sense version:
if (foo === null || typeof foo === "undefined") {
// foo is null or undefined
}
回答by Daniel Vandersluis
JSLint is inherently more defensive than the Javascript syntax allows for.
JSLint 本质上比 Javascript 语法所允许的更具防御性。
From the JSLint documentation:
从 JSLint 文档:
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.When comparing to any of the following values, use the
===
or!==
operators (which do not do type coercion):0 '' undefined null false true
If you only care that a value is truthyor falsy, then use the short form. Instead of
(foo != 0)
just say
(foo)
and instead of
(foo == 0)
say
(!foo)
The
===
and!==
operators are preferred.
在
==
和!=
比较之前运营商做的强制类型转换。这是不好的,因为它导致' \t\r\n' == 0
真实。这可以掩盖类型错误。与以下任何值进行比较时,请使用
===
or!==
运算符(不执行类型强制):0 '' undefined null false true
如果你只关心一个值是真的还是假的,那么使用简写形式。代替
(foo != 0)
说啊
(foo)
而不是
(foo == 0)
说
(!foo)
的
===
和!==
运营商是优选的。
回答by Justin Niessner
Keep in mind that JSLint enforces one persons idea of what good JavaScript should be. You still have to use common sense when implementing the changes it suggests.
请记住,JSLint 强制一个人理解什么是好的 JavaScript。在实施建议的更改时,您仍然必须使用常识。
In general, comparing type and value will make your code safer (you will not run into the unexpected behavior when type conversion doesn't do what you think it should).
通常,比较类型和值将使您的代码更安全(当类型转换没有按照您认为应该做的事情进行时,您不会遇到意外行为)。
回答by Spudley
Triple-equal is different to double-equal because in addition to checking whether the two sides are the same value, triple-equal also checks that they are the same data type.
Triple-equal 与 double-equal 不同,因为除了检查两侧是否为相同的值之外,triple-equal 还会检查它们是否为相同的数据类型。
So ("4" == 4)
is true, whereas ("4" === 4)
is false.
所以("4" == 4)
是真的,而是("4" === 4)
假的。
Triple-equal also runs slightly quicker, because JavaScript doesn't have to waste time doing any type conversions prior to giving you the answer.
Triple-equal 的运行速度也稍快一些,因为 JavaScript 在给出答案之前不必浪费时间进行任何类型转换。
JSLint is deliberately aimed at making your JavaScript code as strict as possible, with the aim of reducing obscure bugs. It highlights this sort of thing to try to get you to code in a way that forces you to respect data types.
JSLint 有意使您的 JavaScript 代码尽可能严格,目的是减少晦涩的错误。它强调了这类事情,试图让您以一种迫使您尊重数据类型的方式进行编码。
But the good thing about JSLint is that it is just a guide. As they say on the site, it will hurt your feelings, even if you're a very good JavaScript programmer. But you shouldn't feel obliged to follow its advice. If you've read what it has to say and you understand it, but you are sure your code isn't going to break, then there's no compulsion on you to change anything.
但是 JSLint 的好处在于它只是一个指南。正如他们在网站上所说的那样,即使您是一个非常优秀的 JavaScript 程序员,它也会伤害您的感情。但是你不应该觉得有义务听从它的建议。如果您已经阅读了它的内容并且理解了它,但是您确定您的代码不会被破坏,那么您就没有必要进行任何更改。
You can even tell JSLint to ignore categories of checks if you don't want to be bombarded with warnings that you're not going to do anything about.
如果您不想被警告轰炸,您甚至可以告诉 JSLint 忽略检查类别,如果您不想做任何事情的话。
回答by Lekensteyn
A quote from http://javascript.crockford.com/code.html:
引自http://javascript.crockford.com/code.html:
=== and !== Operators.
It is almost always better to use the === and !== operators. The == and != operators do type coercion. In particular, do not use == to compare against falsy values.
=== 和 !== 运算符。
使用 === 和 !== 运算符几乎总是更好。== 和 != 运算符进行类型强制。特别是,不要使用 == 与虚假值进行比较。
JSLint is very strict, their 'webjslint.js' does not even pass their own validation.
JSLint 非常严格,他们的'webjslint.js' 甚至没有通过他们自己的验证。
回答by nano2nd
If you want to test for falsyness. JSLint does not allow
如果你想测试虚假。JSLint 不允许
if (foo == null)
but does allow
但确实允许
if (!foo)
回答by EM-Creations
To help explain this question and also explain why NetBeans (from) 7.3 has started showing this warning this is an extract from the response on the NetBeans bug tracker when someone reported this as a bug:
为了帮助解释这个问题并解释为什么 NetBeans(来自)7.3 开始显示此警告,这是从 NetBeans 错误跟踪器上有人报告此错误时的响应中摘录的:
It is good practice to use === rather than == in JavaScript.
在 JavaScript 中使用 === 而不是 == 是一种很好的做法。
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. JSLint cannot reliably determine if == is being used correctly, so it is best to not use == and != at all and to always use the more reliable === and !== operators instead.
== 和 != 运算符在比较之前进行类型强制。这很糟糕,因为它导致 ' \t\r\n' == 0 为真。这可以掩盖类型错误。JSLint 无法可靠地确定 == 是否被正确使用,因此最好根本不要使用 == 和 !=,而是始终使用更可靠的 === 和 !== 运算符。
回答by Rushyo
Well it can't really cause problems, it's just giving you advice. Take it or leave it. That said, I'm not sure how clever it is. There may well be contexts in which it doesn't present it as an issue.
嗯,它不会真正引起问题,它只是给你建议。要么接受,要么离开它。也就是说,我不确定它有多聪明。很可能在某些情况下它不会将其作为问题呈现。