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

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

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

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


当前回答

為什麼 ==是如此不可預測的?

如果你比较一个空的字符串“”与零的数字,你会得到什么?

它没有结束,这里还有一个:

'0' == false // true

[1] == true // true
[] == false // true
[[]] == false // true
[0] == false // true

随后,用绳子

[1,2,3] == '1,2,3' // true - REALLY?!
'\r\n\t' == 0 // true - Come on!

它变得更糟:

let A = ''  // empty string
let B = 0   // zero
let C = '0' // zero string

A == B // true - ok... 
B == C // true - so far so good...
A == C // **FALSE** - Plot twist!

让我们再说一遍:

(A == B) && (B == C) // true
(A == C) // **FALSE**

而这只是你与原始人得到的疯狂事物。

為什麼會發生這件事?

好吧,这是因为不同于“三重平等”(===),这只是检查两个值是否相同。

它有特殊的处理功能,特殊的处理零,无定义,线条,你命名它。

它变得相当可口可乐。

function isEqual(x, y) { // if `==` were a function
    if(typeof y === typeof x) return y === x;
    // treat null and undefined the same
    var xIsNothing = (y === undefined) || (y === null);
    var yIsNothing = (x === undefined) || (x === null);

    if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);

    if(typeof y === "function" || typeof x === "function") {
        // if either value is a string 
        // convert the function into a string and compare
        if(typeof x === "string") {
            return x === y.toString();
        } else if(typeof y === "string") {
            return x.toString() === y;
        } 
        return false;
    }

    if(typeof x === "object") x = toPrimitive(x);
    if(typeof y === "object") y = toPrimitive(y);
    if(typeof y === typeof x) return y === x;

    // convert x and y into numbers if they are not already use the "+" trick
    if(typeof x !== "number") x = +x;
    if(typeof y !== "number") y = +y;
    // actually the real `==` is even more complicated than this, especially in ES6
    return x === y;
}

function toPrimitive(obj) {
    var value = obj.valueOf();
    if(obj !== value) return value;
    return obj.toString();
}

那么这意味着什么呢?

这意味着你可以用错误结束。

让你的生活更不复杂。

使用 === 而不是 ==。

其他回答

在典型的脚本中不会有性能差异 更重要的是,一千个“==="比一千个“=="=====================================================

这个建议不是因为性能问题,而是因为类型强迫意味着(‘\t\r\n' == 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

意思是平等 没有类型强制性 类型强制性 意思是 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 

总是使用“===”并且你会避免成千上万的错误. 如今使用三重平等是由不同的风格指南更受欢迎,因为它比较考虑到类型的歌手。

使用 === 如果你想在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);