【发布时间】:2019-12-07 03:53:56
【问题描述】:
当我们使用小于或等于运算符时,这是在抽象关系比较算法的底层工作。 例如。
a <= b;
像这样转换成 JavaScript
!(b < a)
EcmaScript Spesification 说 (http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5) 表示至少有一个操作数的 NaN 小于 return undefined 这就是意义
var a = 1;
var b = "asd"
a < b // b.toNumber() => NaN and this is operation return undefined (false)
如果我们这样使用
var a = 1;
var b = "asd"
a <= b // this convert to !(b < a) and (b<a) return undefined
// and !(undefined) must be true
但是 EcmaScript 规范说这是返回 false。这对我来说很有趣这是什么原因?
【问题讨论】:
-
仔细查看step 6 of the Less-than-or-equal Operator (
<=): "如果r是true或undefined,则返回false。否则,返回@ 987654332@."
标签: javascript type-conversion ecmascript-5 type-coercion