应该在JavaScript比较中使用哪个等于运算符(== vs ===)? [英] Which equals operator (== vs ===) should be used in JavaScript comparisons?

查看:186
本文介绍了应该在JavaScript比较中使用哪个等于运算符(== vs ===)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JSLint 来浏览JavaScript,并且它会返回许多建议来替换 == (两个等号)与 === (三个等号)做比较 idSele_UNVEHtype.value.length == 0 if 语句中。

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement.

=== 替换 == 是否有性能优势?

Is there a performance benefit to replacing == with ===?

任何性能改进都会受到欢迎,因为存在许多比较运算符。

Any performance improvement would be welcomed as many comparison operators exist.

如果没有进行类型转换,有超过 == 的性能提升?

If no type conversion takes place, would there be a performance gain over ==?

推荐答案

身份( === )运算符的行为与相等( == )运算符完全相同,但不进行类型转换,并且类型必须相同才能被认为是相同的。

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

参考: Javascript教程:比较运算符

== 运算符将进行比较在进行任何必要的类型转换后等式 === 运算符执行转换,因此如果两个值不是同一类型 === 只会返回 false 。两者都同样快。

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. Both are equally quick.

引用道格拉斯·克罗克福德的优秀 JavaScript:好的部分

To quote Douglas Crockford's excellent JavaScript: The Good Parts,


JavaScript有两组相等运算符: == = !== ,以及他们的邪恶双胞胎 == != 。好的工作方式与您期望的方式相同。如果两个操作数具有相同的类型且具有相同的值,则 === 生成 true !== 生成 false 。当操作数属于同一类型时,邪恶的双胞胎做正确的事,但如果它们属于不同的类型,它们会试图强迫价值观。他们这样做的规则是复杂和不可取的。这些是一些有趣的案例:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true

缺乏传递性令人震惊。我的建议是永远不要使用邪恶的双胞胎。相反,请始终使用 === !== 。刚刚显示的所有比较都使用 === 运算符生成 false

The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.






更新:



一个好点是由<评论和 @Casebash > @Phillipe Laybaert的答案涉及参考类型。对于参考类型 == === 彼此一致行动(特殊情况除外)。


Update:

A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning reference types. For reference types == and === act consistently with one another (except in a special case).

var a = [1,2,3];
var b = [1,2,3];

var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };

var e = "text";
var f = "te" + "xt";

a == b            // false
a === b           // false

c == d            // false
c === d           // false

e == f            // true
e === f           // true

特殊情况是,当您将文字与评估为同一文​​字的对象进行比较时,由于其 toString valueOf 方法。例如,考虑字符串文字与 String 构造函数创建的字符串对象的比较。

The special case is when you compare a literal with an object that evaluates to the same literal, due to its toString or valueOf method. For example, consider the comparison of a string literal with a string object created by the String constructor.

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false

这里 == 运算符正在检查两个对象的值并返回 true ,但是 === 看到它们的类型不同并返回。哪一个是正确的?这真的取决于你想要比较的东西。我的建议是完全绕过这个问题,不要使用 String 构造函数来创建字符串对象。

Here the == operator is checking the values of the two objects and returning true, but the === is seeing that they're not the same type and returning false. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String constructor to create string objects.

参考

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

这篇关于应该在JavaScript比较中使用哪个等于运算符(== vs ===)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆