我使用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();
}

那么这意味着什么呢?

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

让你的生活更不复杂。

使用 === 而不是 ==。

其他回答

如果您正在创建一个网页应用程序或安全的页面,您应该始终使用(只有在可能的情况下)

===

因为它会检查它是否是相同的内容,是否是相同的类型!

当有人进来时:

var check = 1;
if(check == '1') {
    //someone continued with a string instead of number, most of the time useless for your webapp, most of the time entered by a user who does not now what he is doing (this will sometimes let your app crash), or even worse it is a hacker searching for weaknesses in your webapp!
}

但与

var check = 1;
if(check === 1) {
    //some continued with a number (no string) for your script
} else {
    alert('please enter a real number');
}

黑客将永远不会深入到系统中找到错误并攻击您的应用程序或用户

我的观点是,

===

将为您的脚本添加更多安全性

当然,你也可以检查输入的号码是否有效,是行,等等等,如果在第一例内的声明,但这至少对我来说更容易理解和使用。

我发布的原因是“更安全”或“安全”这个词从未在这个对话中说过(如果你看 iCloud.com 它使用 2019 次 === 和 1308 次 ==,这也意味着你有时会使用 == 而不是 === 因为它否则会阻止你的功能,但正如你说的那样,你应该使用 === 尽可能多)

使用一个未提到的理由 === - 是你与 / 交叉编写到 / 从咖啡脚本共存的情况。

JavaScript 的弱平等比较有某种混乱的行为,并且往往是混乱的错误的来源。 解决方案是使用严格的平等运营商,由三个平等标志组成: ===. 它就像正常的平等运营商一样工作,但没有任何类型的强迫。

如果你经常转换到和从咖啡脚本,你应该只使用 ===。

CoffeeScript 解决了这一点,简单地将所有弱的比较与严格的比较相替代,换句话说,将所有 ==比较器转换为 ===. 在 CoffeeScript 中,你不能进行弱的平等比较,并且在需要时比较之前,你应该明确地转换类型。

平等比较运营商 ==是混乱的,应该避免。

如果你必須與它一起生活,請記住以下三件事:

它不是过渡的:(a ==b)和(b ==c)不会导致(a ==c)它是相互排斥它的否认:(a ==b)和(a!=b)总是保持相反的布莱恩值,所有A和B。

平等运营商在JavaScript中的真实表格

表中的每个行都是三个相互“平等”值的组合,这意味着它们之间的任何2个值都是平等的,使用平等的 ==标志*。

** 奇怪:请注意,第一列中的任何两个值在这个意义上不平等。

''       == 0 == false   // Any two values among these 3 ones are equal with the == operator
'0'      == 0 == false   // Also a set of 3 equal values, note that only 0 and false are repeated
'\t'     == 0 == false   // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\r'     == 0 == false   // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\n'     == 0 == false   // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\t\r\n' == 0 == false   // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

null == undefined  // These two "default" values are not-equal to any of the listed values above
NaN                // NaN is not equal to any thing, even to itself.

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

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

例如,如果你有:

$a = 0;

然后:

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

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

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

$a = 0;

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

因此,新 String(“Hello world”) ===“Hello world”返回虚假,但是, ==不关心如果对象是相同的,它只是将一个论点转换为另一个类型:如果转换是不可能的,返回虚假。