If you call Object.prototype.toString.call(anything)
the result is always [object Something]
, where Something
could be one of several things. My question is why is the "object" part there? It seems superfluous to always have it there. It doesn't tell you anything about the argument which was passed in.
Object.prototype.toString.call(null);
=> [object Null]
Object.prototype.toString.call(undefined);
=> [object Undefined]
Since null
and undefined
aren't objects (and fails CheckObjectCoercible
), the "object" part really, really seems meaningless.
I have to think there was some reason originally that the "object" part was put there, even if the reason has been lost in the time since, and it's now just preserved for historical reasons.
Can anyone help shed some light on this?
To be clear, I already know how Object.prototype.toString
can be used to get the [[Class]]
(type) of an object. My question concerns the reason for the format of the returned string -- specifically the "object" part at the beginning. I want to know if there was ever a reason for this part or if there is a possible future reason for this part. Maybe in the future it could return something other than "object"? Can I expect it to always return "object"? If so, why does it even return it? Who wants a function to always return the same thing no matter what the input is?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…