Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript比较运算符:标识与相等_Javascript_Operators_Comparison Operators_Equivalence - Fatal编程技术网

JavaScript比较运算符:标识与相等

JavaScript比较运算符:标识与相等,javascript,operators,comparison-operators,equivalence,Javascript,Operators,Comparison Operators,Equivalence,我一直在试图理解JavaScript的比较运算符之间的区别:标识和相等。从我读到的内容来看,如果您使用==检查两个对象的相等性,JavaScript将尝试找出它们是否是相同的类型,如果不是,则尝试将它们转换为相同的类型。但是,===的行为方式不同。例如: var n = "1"; console.log(n==1); // outputs true console.log(n===1); // outputs false 那么,这些“标识”运算符和常规相等运算符之间的

我一直在试图理解JavaScript的比较运算符之间的区别:标识和相等。从我读到的内容来看,如果您使用==检查两个对象的相等性,JavaScript将尝试找出它们是否是相同的类型,如果不是,则尝试将它们转换为相同的类型。但是,===的行为方式不同。例如:

var n = "1";
console.log(n==1);        // outputs true
console.log(n===1);       // outputs false
那么,这些“标识”运算符和常规相等运算符之间的区别是什么呢?两者兼有的好处是什么

在表现上有差异吗?我认为identity操作符会更快,因为它不进行转换


此外,当涉及到更复杂的对象(如数组)时,它们之间有何区别?最重要的是,关于何时应该使用一种方法而不是另一种方法,约定是怎么说的?为什么?

区别在于==、=和!=将执行类型强制-例如,强制将字符串作为数字求值。==,==,而且!==不会执行类型强制。他们会将字符串与数字进行比较,由于字符串“1”与数值1不同,因此结果为false

参考资料如下:

在进行比较之前,相等运算符将尝试使数据类型相同。另一方面,identity操作符要求两种数据类型作为前提条件相同

还有很多其他类似的帖子。见:

(有一个很好的比较表)

在实践中,当您想要确定布尔值为真或假时,标识运算符非常方便,因为

1 == true     => true
true == true  => true
1 === true    => false
true === true => true

=
==
相同,只是
=
进行类型转换

为了向您展示我在这里的意思,有一个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();
}
这个函数应该有助于解释为什么人们总是说你不应该使用
==

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

以下是一些您意想不到的结果示例:

意外的真相

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

'\r\n\t' == 0 // returns true
// 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**
// 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**
意外结论

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

'\r\n\t' == 0 // returns true
// 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**
// 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**
具有特殊功能的对象

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

'\r\n\t' == 0 // returns true
// 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**
// 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**

等式运算符和恒等运算符特别值得注意。相等运算符将尝试强制(转换)操作数为相同类型以评估相等性。这是一个方便的功能,只要你知道它正在发生。这段代码显示了相等运算符的作用

let firstVal = 5;
let secondVal = "5";
if (firstVal == secondVal) {
console.log("They are the same");
} else {
console.log("They are NOT the same");
}
此脚本的输出如下所示:

它们是一样的

JavaScript正在将两个操作数转换为同一类型并进行比较。本质上,相等运算符测试值是否相同,而不考虑其类型

如果要测试以确保值和类型相同,则需要使用标识运算符(==,三个等号,而不是相等运算符中的两个)

在此示例中,标识运算符将考虑两个变量不同。此运算符不强制类型。此脚本的结果如下所示:

它们不一样


知道了。。。因此,它就像我上面概述的那样简单。identity运算符不检查类型,但equality运算符检查类型。至于何时使用哪一个;:使用===和!==几乎总是更好的接线员。==和!=运算符执行类型强制。特别是,不要使用==与falsy值进行比较。@Joel。。。棒极了的链接!谢谢但是,它没有列出==。那些是合法的吗?@Tim Down,在你的例子中,这是不必要的,但这是错误的吗?无论如何,使用===有缺点吗?除了这个例子之外,我唯一能想到的是强制显式强制转换/强制。但是“==”不存在。这里我为JavaScript中的equal运算符提供了一个真值表