JavaScript中==和===运算符的确切行为
在用JavaScript编写用于比较的简单代码段时,我观察到一些奇怪的行为 案例1:JavaScript中==和===运算符的确切行为,javascript,Javascript,在用JavaScript编写用于比较的简单代码段时,我观察到一些奇怪的行为 案例1: typeof(window.WHTStatement.DDL_TPTypeID.size()) ==> "number" typeof(window.WHTStatement.Txt_TPTypeValue.size()) ==> "number" window.WHTStatement.DDL_TPTypeID.size() == 1 == window.WHTStatement.Txt_TPT
typeof(window.WHTStatement.DDL_TPTypeID.size()) ==> "number"
typeof(window.WHTStatement.Txt_TPTypeValue.size()) ==> "number"
window.WHTStatement.DDL_TPTypeID.size() == 1 == window.WHTStatement.Txt_TPTypeValue.size()
返回true——确定
案例2:
window.WHTStatement.DDL_TPTypeID.size() === 1 == window.WHTStatement.Txt_TPTypeValue.size()
返回true——确定
案例3:
window.WHTStatement.DDL_TPTypeID.size() === 1 === window.WHTStatement.Txt_TPTypeValue.size()
返回false,为什么
案例3中到底发生了什么。有人能详细说明吗?与Python不同,在JS中,x==y==z不等于
x==y&&y==z
,而是(x==y)==z
。所以你实际上是在比较一个布尔值和一个在类型检查中明显失败的数字
=
比较有效,因为1==true
是true
与Python不同,在JSx==y==z
中,x==y==z不等于x==y&&y==z
。所以你实际上是在比较一个布尔值和一个在类型检查中明显失败的数字
=
比较有效,因为1==true
是true
这可能有助于它不是“怪异”行为;“奇怪”的行为和你——而不是其他任何人——的行为是有区别的没想到。这可能有助于它不是“奇怪”的行为;“奇怪”的行为和你——而不是其他任何人——的行为是有区别的没想到。