I am writing JavaScript interpreter in Python and I have to understand internals. Consider this code (tested on V8):
Object.prototype.toString(new Number(5)) //gives "[object Object]"
According to the specification of Number constructor:
"The [[Class]] internal property of the newly constructed object is set to "Number"."
And Object.prototype.toString returns the combination of:
"[object ", class, and "]" // where class is the value of [Class]] internal property of O.
Therefore why the returned value is "[object Object]" instead of "[object Number]"? Is it a bug in V8 or my understanding is wrong?
toStringdoesn't take an argument -- it's a method on the object. So if you callObject.prototype.toString.call(new Number(5))(thus passing the Number instance asthis) you'll get the expected result:[object Number].You get similarly bogus results when calling
SomeClass.prototype.toStringwith an argument, for exampleNumber.prototype.toString(new Number(5))will give'0'.I tested all of this on node (which uses v8).