[英]The Less-than-or-equal Operator: With NaN
When we use The Less-than-or-equal Operator this is work under the hood with The Abstract Relational Comparison Algorithm. 当我们使用小于或等于运算符时,这是使用“抽象关系比较算法”进行的。 For example.
例如。
a <= b;
Convert to JavaScript like this 像这样转换为JavaScript
!(b < a)
And EcmaScript Spesification says ( http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5 ) which indicates that at least one operand is NaN less than return undefined And this is meaning 而且EcmaScript Spesification说( http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5 )表示至少一个操作数比返回undefined少NaN,这意味着
var a = 1;
var b = "asd"
a < b // b.toNumber() => NaN and this is operation return undefined (false)
If we use like this 如果我们这样使用
var a = 1;
var b = "asd"
a <= b // this convert to !(b < a) and (b<a) return undefined
// and !(undefined) must be true
But EcmaScript spesification says this is return false. 但是EcmaScript spesification说这是返回false。 This is interesting for me what is reason this?
这对我来说很有趣,这是什么原因?
While <= does use the Abstract Relational Comparison Algorithm, a <= b
isn't equivalent to !(b < a)
. 虽然<=确实使用了抽象关系比较算法,
a <= b
不等于!(b < a)
。 It is equivalent to b < a !== false ? false : true
等效于
b < a !== false ? false : true
b < a !== false ? false : true
(where <
represents the Abstract Relational Comparison Algorithm, not the JavaScript <
operator which can never evaluate to undefined
), which behaves the same as !(b < a)
when b < a
is truthy or exactly false
, but does not behave the same when b < a
is falsey in general. b < a !== false ? false : true
(其中<
表示抽象关系比较算法,而不是JavaScript <
运算符,该运算符永远无法求值undefined
),当b < a
为真或完全为false
时,其行为与!(b < a)
相同,但不为false
当b < a
通常为假时,行为相同。 If b < a
evaluates to undefined
, the whole expression will evaluate to false
. 如果
b < a
为undefined
,则整个表达式的结果为false
。
This is defined in the spec at step 6 here: https://www.ecma-international.org/ecma-262/5.1/#sec-11.8.3 规范在此处的第6步中进行了定义: https : //www.ecma-international.org/ecma-262/5.1/#sec-11.8.3
- Let r be the result of performing abstract relational comparison rval < lval with LeftFirst equal to false .
令r为在LeftFirst等于false的情况下执行抽象关系比较rval < lval的结果。
- If r is true or undefined , return false .
如果r为true或undefined ,则返回false 。 Otherwise, return true .
否则,返回true 。
The Abstract Relational Comparison Algorithm can only evaluate to true
, false
, or undefined
; 抽象关系比较算法只能计算
true
, false
或undefined
; so the "Otherwise" in step 6 can only apply when r
is false
, making a <= b
equivalent to b < a !== false ? false : true
因此,步骤6中的“否则”仅在
r
为false
时才适用,从而使a <= b
等效于b < a !== false ? false : true
b < a !== false ? false : true
(again, where <
represents the Abstract Relational Comparison Algorithm). b < a !== false ? false : true
(再次,其中<
表示抽象关系比较算法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.