查看此代码示例或转到 jsfiddle
function printRelation(a, b, out) {
var text;
if (a === b) {
text = "a === b";
} else if (a == b) {
text = "a == b";
} else {
text = "a != b";
}
$('#' + out).text(text);
}
var a = [0, 0, 2], b = a;
printRelation(a, b, 'out1');
a = [0, 0, 2];
b = [0, 0, 2];
printRelation(a, b, 'out2');
我本来希望这两个测试都能输出a === b
,但只有第一个可以。第二个输出a != b
。谁能解释这种行为?如何有效地比较javascript中的数组?