为什么等于运算符不将从 valueOf() 获得的 null 转换为零?

Why the operator equal is not converting null obtained from valueOf() to zero?

在最后一行,我知道 a.valueOf() 被调用是因为操作数的类型不同。知道 null 转换为零,为什么浏览器不会将 null 转换为零,最后 return false?

function A(){}
A.prototype.valueOf = function(){return null}
var a = new A
a == null // => This is always false
a == 0 // => true or false depending on the browser you are using.

I know that valueOf() is used because the operands are not of the same type

一般不会"when they are not of the same type",不会。 ToPrimitive(以及随后的 [[DefaultValue]].valueOf)仅在将布尔值、数字或字符串与对象进行比较时调用。

null converts to zero

是的,当调用 ToNumber 时。例如,当调用 ToString 时它不会。

Why a browser would not convert null to zero and return false at the end?

因为 null0 是不同的基本类型,在 Abstract Equality Comparison Algorithm 中没有指定转换,所以没有调用 ToNumber。他们根本不相等,point.

但是在 Firefox 中,a bug 有这个。