javascript-不同类型(<和>)进行比较的规则是什么?

对于<运算符,将其应用于两个字符串,两个数字和两个布尔值似乎很容易理解. 我的问题分为两部分: 当我们使用它比较两个对象时有什么规则? 看来我不能写{}< {},但以下代码生成false.

var a = {};
var b = {};
console.log(a < b); //false

当我们使用它比较两个不同类型时有什么规则?

true < "" //false
true > "" //true
10 < true //false
10 < "" //false

我不知道为什么结果是这样的.我找到an article on comparison in JavaScript,但没有详细信息.

顺便说一句,我对==和===的工作方式毫无疑问.

解决方法:

What is the rule when we use it to compare two objects?

wikipedia开始:

When comparing variables which are objects they are considered to be
different if their objects are not the same object, even if the values
of them are the same, so:

var obj1 = {a: 1};
var obj2 = {a: 1};
var obj3 = obj1;
alert(obj1 == obj2);   //false
alert(obj3 == obj1);   //true

What is the rule when we use it to compare two different type?

在您发布的文章中对此进行了解释,我引用其中的内容:

If the two operands are not of the same type, JavaScript converts the
operands, then applies strict comparison. If either operand is a
number or a boolean, the operands are converted to numbers if
possible; else if either operand is a string, the string operand is
converted to a number if possible. If both operands are objects, then
JavaScript compares internal references which are equal when operands
refer to the same object in memory.

上一篇:[Algorithm] Bitwise Operators


下一篇:C#-是运算符-检查所有可用转换的可铸性