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

那么这意味着什么呢?

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

让你的生活更不复杂。

使用 === 而不是 ==。

其他回答

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

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

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

CString x;
delete x;

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

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

操作员 = = = = = = = = = = = = = = = = = = = = = = = = = = =

此分類上一篇

缺乏过渡性是令人担忧的,我的建议是永远不要使用邪恶的双胞胎,相反,总是使用 === 和!== 所有仅仅显示的比较都是与 === 运营商虚假的。


更新:

var a = [1,2,3];
var b = [1,2,3];

var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };

var e = "text";
var f = "te" + "xt";

a == b            // false
a === b           // false

c == d            // false
c === d           // false

e == f            // true
e === f           // true

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false

在这里 ==运营商正在检查两个对象的值,并返回真实,但 ===看到它们不是相同的类型,并返回虚假。 哪一个是正确的? 这真的取决于你正在试图比较什么。

参考 http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

简单

= = = = = = = = = = = = = = = = = = = = = =

=== 是指在没有类型强迫的情况下进行操作的比较。

格式强制在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.

原因在上面的答案中被解释,而且杜格拉斯·克罗克福德也对此非常清楚(JavaScript:The Good Parts)。

但是,有一個例外: == null 是檢查“是零或不定義”的有效方式:

if( value == null ){
    // value is either null or undefined
}

例如,jQuery 1.9.1 使用此模式 43 次,而 JSHint 合成检查器甚至为此提供 eqnull 放松选项。

从 jQuery 风格指南:

严格的平等检查(===)应用于对 ==的支持,唯一的例外是通过零来检查非定义和零。

// Check for both undefined and null values, for some important reason. 
undefOrNull == null;

编辑 2021-03:

如今,大多数浏览器支持 Nullish coalescing 操作器(??) 和 Logical nullish 任务(??=),如果变量是 null 或 undefined,例如:

if (a.speed == null) {
  // Set default if null or undefined
  a.speed = 42;
}

可以用任何形式写作。

a.speed ??= 42;
a.speed ?? a.speed = 42;
a.speed = a.speed ?? 42;

在您的使用中,不可能有两项操作之间的性能差异. 没有类型转换要进行,因为两项参数已经是相同的类型. 两项操作将有一个类型比较,其次是值比较。