JavaScript比较运算符:Identity与Equality

Hri*_*sto 19 javascript operators equivalence comparison-operators

我一直在努力理解JavaScript的比较运算符之间的区别:身份和平等.根据我的阅读,如果使用==检查两个对象的相等性,JavaScript将尝试确定它们是否是相同类型,如果不是,则尝试将它们设置为相同类型.但是,===的行为方式不同.举个例子:

var n = "1";
console.log(n==1);        // outputs true
console.log(n===1);       // outputs false
Run Code Online (Sandbox Code Playgroud)

那么这些"身份"运营商和正规平等运营商之间的区别是什么?两者兼有的好处是什么?

性能有差异吗?我认为身份运算符会更快,因为它不会进行转换.

另外,当涉及更复杂的对象(如数组)时,它们有何不同?最重要的是,公约会说什么时候应该使用另一个,为什么?

Mic*_*and 30

在进行比较之前,相等运算符将尝试使数据类型相同.另一方面,身份运算符要求两种数据类型都与先决条件相同.

还有很多其他帖子类似于这个问题.看到:

PHP等式(== double equals)和identity(=== triple equals)比较运算符有何不同?(有一个很好的比较图表)
在JavaScript比较中应该使用哪个等于运算符(== vs ===)?

在实践中,当您想要确定布尔值为true或false时,身份运算符非常方便,因为......

1 == true     => true
true == true  => true
1 === true    => false
true === true => true
Run Code Online (Sandbox Code Playgroud)


Joe*_*orn 16

区别在于==,<=,> =和!=将执行类型强制 - 例如,强制将字符串计算为数字.===,<==,> ==,并且!==不会进行类型强制.他们会将字符串与数字进行比较,由于字符串"1"与数字值1不同,因此结果为false.

参考文献:https:
//developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

  • 至于何时使用哪一个; [Douglas Crockford](http://javascript.crockford.com/code.html):*使用===和!==运算符几乎总是更好.==和!=运算符会输入强制.特别是,不要使用==来比较虚假值.* (2认同)
  • 但是 '&lt;==' 和 '&gt;==' 不存在。 (2认同)

Lui*_*rez 10

==是相同的东西===,只是==不类型转换

为了向您展示我的意思,这是一个JavaScript函数,其行为完全如下==:

// loseEqual() behaves just like `==`
function loseEqual(x, y) {
    // notice the function only uses "strict" operators 
    // like `===` and `!==` to do comparisons

    if(typeof y === typeof x) return y === x;

    if(typeof y === "function" || typeof x === "function") return false;

    // 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 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;

    return x === y;
}

function toPrimitive(obj) {
    var value = obj.valueOf();
    if(obj !== value) return value;
    return obj.toString();
}
Run Code Online (Sandbox Code Playgroud)

这个功能应该有助于解释为什么人们一直说你不应该使用==.

正如您所看到的==,类型转换有很多复杂的逻辑.因此,很难预测你会得到什么结果 - 这可能会导致错误.

以下是您不期望的一些结果的一些示例:

出乎意料的真相

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

'\r\n\t' == 0 // returns true
Run Code Online (Sandbox Code Playgroud)

意外的结论

// IF an empty string '' is equal to the number zero (0)
'' == 0 // return true

// AND the string zero '0' is equal to the number zero (0)
'0' == 0 // return true

// THEN an empty string must be equal to the string zero '0'
'' == '0' // returns **FALSE**
Run Code Online (Sandbox Code Playgroud)

具有特殊功能的对象

// Below are examples of objects that
// implement `valueOf()` and `toString()`

var objTest = {
    toString: function() {
        return "test";
    }
};

var obj100 = {
    valueOf: function() {
        return 100;
    }
};

var objTest100 = {
    toString: function() {
        return "test";
    },
    valueOf: function() {
        return 100;
    }
};

objTest == "test" // returns true
obj100 == 100 // returns true
objTest100 == 100 // returns true

objTest100 == "test" // returns **FALSE**
Run Code Online (Sandbox Code Playgroud)