我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
在JavaScript中,它具有相同的值和类型。
例如,
4 == "4" // will return true
但
4 === "4" // will return false
其他回答
简单
= = = = = = = = = = = = = = = = = = = = = =
和
=== 是指在没有类型强迫的情况下进行操作的比较。
格式强制在JavaScript中意味着自动将数据类型转换为其他数据类型。
例如:
123 == "123" // Returns true, because JS coerces string "123" to number 123
// and then goes on to compare `123 == 123`.
123 === "123" // Returns false, because JS does not coerce values of different types here.
我在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),和代码质量是。
不同之間的 =, =, = =
= 操作员 只用来指定值 = = 操作员 只用来比较不数据型值 = = 操作员 只用来比较值和数据型值。
var a = new String("123");
var b = "123";
alert(a === b); // returns false !! (but they are equal and of the same type)
看到这一点在一个答案. a 和 b 不是真正相同的类型在这种情况下,如果你检查 tipof(a) 你会得到“对象” 和 tipof(b) 是“紧张”。
意思是平等 没有类型强制性 类型强制性 意思是 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