Given that a literal number not strictly an instance of Number
, why can I call prototype methods of Number
(or String
, or Boolean
) objects on the corresponding literal objects? Is this standard behavior across browsers?
What exactly is happening when this occurs? I suspect it's coercing the literal into the corresponding type before calling the method, because when I inspect typeof this
in the method, it's returning 'object'
rather than 'number'
.
number
. For an instance of something, it would need to be of typeobject
. Sotypeof 3 = number
,typeof new Number(3) = object
, You can coerce a built in type 'number, string' etc, byObject(3) instanceof Number
, this would return true, you could also do for other built's like String etc. or eithervar x = 3; x = Object(x);
x is now instanceof Number. – Ysabelfunction isNumber(n) { return typeof n === 'number' || n instanceof Number; }
– Ysabelfunction isNumber(n) { return Object.prototype.toString.call(n) === '[object Number]'; }
, which works for number primitives, number intances, and number instances from other windows. It's also the default implementation for shims like Array.isArray. – Depressionisn't safe across browser windows
, isn't that the case ofinstanceof
that your talking about here?.. You mentioned nothing about IPC between windows / iframes. So I'm a little confused why your even talking about instanceof in this respect. – Ysabel