Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

javascript - JavaScript比较中应使用哪个等于运算符(== vs ===)?(Which equals operator (== vs ===) should be used in JavaScript comparisons?)

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.

(我使用JSLint的经历JavaScript和它的返回许多建议更换== (双等号)与=== (三级等号)做这样的事情进行比较时idSele_UNVEHtype.value.length == 0内部的if声明。)

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 == ?

(如果没有进行类型转换,是否会获得==以上的性能提升?)

  ask by bcasp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

Reference: Javascript Tutorial: Comparison Operators

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

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 .

(===运算符将进行转换,因此,如果两个值不同,则类型===只会返回false 。)

Both are equally quick.

(两者都同样快。)

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

(引用Douglas Crockford出色的JavaScript:The Good Parts ,)

JavaScript has two sets of equality operators: === and !== , and their evil twins == and != .

(JavaScript有两组相等运算符: ===!== ,以及它们的邪恶双胞胎==!= 。)

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 .

(如果两个操作数具有相同的类型并且具有相同的值,则===会生成true!==会生成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 

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.

(刚刚显示的所有比较都使用===运算符生成了false 。)


Update: (更新:)

A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning reference types.

(@Casebash在评论中和@Phillipe Laybaert关于引用类型 答案中提出了一个很好的观点。)

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

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.

(特殊情况是将文字与对象结果相同的文字进行比较,这是由于其toStringvalueOf方法。)

For example, consider the comparison of a string literal with a string object created by the String constructor.

(例如,考虑将字符串文字与由String构造函数创建的字符串对象进行比较。)

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

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 .

(这里==运算符正在检查两个对象的值并返回true ,但是===看到它们不是同一类型并返回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.

(我的建议是完全绕过该问题,只是不要使用String构造函数来创建字符串对象。)

Reference

(参考)
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...