我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。

能否取代 = = = = = = =?

任何性能改进都将被欢迎,因为有许多比较运营商。

如果没有类型转换发生,会有超越 ==的性能收益吗?


当前回答

意思是平等 没有类型强制性 类型强制性 意思是 JavaScript 不会自动将任何其他数据类型转换为序列数据类型

0==false   // true,although they are different types

0===false  // false,as they are different types

2=='2'    //true,different types,one is string and another is integer but 
            javaScript convert 2 to string by using == operator 

2==='2'  //false because by using === operator ,javaScript do not convert 
           integer to string 

2===2   //true because both have same value and same types 

其他回答

我在Firefox使用Firebug测试了此类代码:

console.time(“testEquality”); var n = 0; while (true) { n++; if (n == 100000) break; } console.timeEnd(“testEquality”);

console.time(“testTypeEquality”); var n = 0; while (true) { n++; if (n === 100000) break; } console.timeEnd(“testTypeEquality”);

我的结果(每次测试5次,平均):

==: 115.2
===: 114.4

所以我会说小差异(这是超过100000 iterations,记住)是不可忽视的。 性能不是一个理由做 ===. 类型安全(好,安全,你会得到在JavaScript),和代码质量是。

平等比较:

运营商 =

回归是真实的,当两个操作员是平等的时,操作员在比较之前转换为相同的类型。

>>> 1 == 1
true
>>> 1 == 2
false
>>> 1 == '1'
true

平等与类型比较:

主持人 ===

回来是真实的,如果两个操作员是相同的和类型。 一般情况下,如果你比较这个方式,它是更好的和更安全的,因为没有背后的场景类型转换。

>>> 1 === '1'
false
>>> 1 === 1
true

在JavaScript中,它具有相同的值和类型。

例如,

4 == "4" // will return true

4 === "4" // will return false 

JavaScript = = = = = = =

0==false   // true
0===false  // false, because they are of a different type
1=="1"     // true, auto type coercion
1==="1"    // false, because they are of a different type

在您的使用中,不可能有两项操作之间的性能差异. 没有类型转换要进行,因为两项参数已经是相同的类型. 两项操作将有一个类型比较,其次是值比较。