Page 268 of the Es262 spec states:
RelationalExpression : RelationalExpression in ShiftExpression
[...]
Let rref be the result of evaluating ShiftExpression.
Let rval be ? GetValue(rref).
If Type(rval) is not Object, throw a TypeError exception.
So in other words: You can't use in
on numbers. Thats just the way it is defined.
new Number
however does not create a number, but a number object (an object that inherits from the Number.prototype
). That's why you can use in
on it, cause its an actual object.
You can still do 12..toFixed()
, thats because of a very interesting construct in the spec: The abstract GetValue
operation, which will be called when you access a property¹, does call toObject
if the target (12
in this case) is not an object, and that will then do the following:
Return a new Number object whose [[NumberData]] internal slot is set to argument.
So in other words: 12..toFixed()
is exactly the same as new Number(12).toFixed()
.
¹ interestingly accessing the property itself does not actually do that according to the spec, if you do a.b
that will only look up the value of a
and create a reference (Reference(a, "b")
). The actual property lookup happens when GetValue
gets called on it (however I don't know of any case were a Reference gets lost without calling GetValue
on it).