gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:45:36 26 4
gpt4 key购买 nike

对于 <运算符,当它应用于两个字符串、两个数字和两个 bool 值时,它似乎很容易理解。

我的问题分为两部分:

我们用它来比较两个对象的规则是什么?

好像不能写{} < {} , 但以下代码生成 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.

关于javascript - 不同类型的比较(<和>)规则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com