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

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

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

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


当前回答

使用 = = 运营商(平等)

true == 1; //true, because 'true' is converted to 1 and then compared
"2" == 2;  //true, because "2" is converted to 2 and then compared

使用 === 运营商(身份)

true === 1; //false
"2" === 2;  //false

这是因为平等运营商 = = 输入强迫,这意味着翻译者在比较之前暗示地试图转换值。

另一方面,身份操作员 === 不做类型强制,因此在比较时不转换值。

其他回答

是啊!重要。

=== 操作员在 javascript 中检查值以及类型,在那里作为 == 操作员只检查值(需要时进行类型转换)。

此分類上一篇

您可以轻松地测试它. 在 HTML 文件中插入跟踪代码并在浏览器中打开它

<script>

function onPageLoad()
{
    var x = "5";
    var y = 5;
    alert(x === 5);
};

</script>

</head>

<body onload='onPageLoad();'>

现在修改 onPageLoad() 方法以警告(x == 5);你会得到真相。

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

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

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

'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();
}

那么这意味着什么呢?

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

让你的生活更不复杂。

使用 === 而不是 ==。

问题在于,你可能很容易陷入麻烦,因为JavaScript有很多暗示的转换,这意味着...

var x = 0;
var isTrue = x == null;
var isFalse = x === null;

最好的样本为什么暗示转换是“邪恶”可以从这个代码在MFC / C++,这实际上会由一个暗示转换从CString到HANDLE,这是一个指标类型。

CString x;
delete x;

显然,在工作时间内,它会做一些非常不确定的事情......

谷歌对C++和STL的暗示转换,以获得一些反对它的论点。

JavaScript 有严格的和类型转换的比较. 一个严格的比较(例如, ===)仅是真实的,如果操作器是相同的类型. 更常用的抽象比较(例如, ==)在进行比较之前将操作器转换为相同的类型。

平等(==)操作员将操作员转换为操作员,如果它们不属于相同类型,则适用严格的比较;如果操作员是数字或波兰,操作员将转换为数字,如果可能;否则如果操作员是序列,操作员将转换为数字,如果可能。

参考:比较运营商(Mozilla Developer Network)

一个简单的例子是

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

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