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

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

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

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


当前回答

首先,一些关于JavaScript行方程式的术语:双方程式正式称为抽象平等比较运营商,而三方程式则称为严格平等比较运营商。

console.log(3 == "3"); // true
console.log(3 === "3"); // false.
console.log(3 == "3"); // true
console.log(3 === "3"); // false.

使用兩個平等的標誌返回真實,因為字符串“3”在比較之前轉換為3號。

console.log(true == '1'); // true
console.log(true === '1'); // false
console.log(true == '1'); // true
console.log(true === '1'); // false

再一次,抽象的平等比较进行类型转换,在这种情况下,两者都是真实的和“1”字符串转换为1号,结果是真实的。

如果你明白你正好在你的道路上区分 ==和 ===. 但是,有一些场景,其中这些运营商的行为是不直觉的。

console.log(undefined == null); // true
console.log(undefined === null); // false. Undefined and null are distinct types and are not interchangeable.
console.log(undefined == null); // true     
console.log(undefined === null); // false. Undefined and null are distinct types and are not interchangeable.

console.log(true == 'true'); // false. A string will not be converted to a boolean and vice versa.
console.log(true === 'true'); // false
console.log(true == 'true'); // false. A string will not be converted to a boolean and vice versa.
console.log(true === 'true'); // false

下面的例子是有趣的,因为它描述了字符串与字符串对象不同。

console.log("This is a string." == new String("This is a string.")); // true
console.log("This is a string." === new String("This is a string.")); // false
console.log("This is a string." == new String("This is a string.")); // true
console.log("This is a string." === new String("This is a string.")); // false

其他回答

JSLint 有时会给你一些不现实的理由来改变事物。 === 具有与 == 相同的性能,如果类型已经相同。

它只有当类型不相同时更快,在这种情况下它不会尝试转换类型,而是直接返回虚假。

因此,IMHO,JSLint可能用来写新的代码,但无用的过度优化应该以任何代价避免。

也就是说,在一个检查中没有理由改变 == 到 === 如果(一个 == 测试)当你知道它是因为一个可以只是一个线条。

修改大量的代码会浪费开发人员和评论家的时间,并无所得。

这是一个严格的检查测试。

这是一个很好的事情,特别是如果你在0和虚假和零之间检查。

例如,如果你有:

$a = 0;

然后:

$a==0; 
$a==NULL;
$a==false;

所有返回是真实的,你可能不想这样做. 假设你有一个功能可以返回一个序列的 0th 指数或错误的故障. 如果你检查“=="错误,你可以得到一个混乱的结果。

所以,与上面的相同,但严格的测试:

$a = 0;

$a===0; // returns true
$a===NULL; // returns false
$a===false; // returns false

使用 === 如果你想在JavaScript中比较几件事,它被称为严格的平等,这意味着如果只有两种类型和值是相同的,就不会有任何不需要的类型纠正为你,如果你使用 ==,你基本上不关心类型,在许多情况下你可能会遇到问题与空虚的平等比较。

使用严格的平等 ===

var num = 0;
var obj = new String('0');
var str = '0';

console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true

console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false

使用平等 = = =

空的平等比较两种平等值,然后将两种值转换为一个常见的类型。 转换后(一方或两方可能遭受转换),最终的平等比较按 === 进行。 空的平等是相同的: A == B 总是具有相同的序列为 B == A 的任何值 A 和 B (除了应用转换顺序)。

var num = 0;
var obj = new String('0');
var str = '0';

console.log(num == num); // true
console.log(obj == obj); // true
console.log(str == str); // true

console.log(num == obj); // true
console.log(num == str); // true
console.log(obj == str); // true
console.log(null == undefined); // true

// both false, except in rare cases
console.log(obj == null);
console.log(obj == undefined);

上述2个答案都意味着平等,而 ===则意味着身份。

如果两个操作员是对象,那么它们是相似的,以确定它们是否是相同的对象;如果两个操作员指向相同的对象,那么相同的操作员会返回真相;否则,两者不相同。

var a = [1, 2, 3];  
var b = [1, 2, 3];  
console.log(a == b)  // false  
console.log(a === b) // false  

在上面的代码中,既 == 又 === 都是虚假的,因为 a 和 b 不是相同的对象。

也就是说,如果两个操作员是 ==的对象, ==行为相同的 ===,这也意味着身份,这两个操作员的基本区别是类型转换。

一个简单的例子是

2 == '2'  -> true, values are SAME because of type conversion.

2 === '2'  -> false, values are NOT SAME because of no type conversion.