I have the following code
var d = new Date();
Object.prototype.toString(d); //outputs "[object Object]"
Object.prototype.toString.apply(d); //outputs "[object Date]"
Why is this difference and what's going on?
edit:
d.toString() // outputs "Tue Nov 06 2012 ..."
So from where does the Date in "[object Date]" comes from. Is it the native code of the browser that do the trick?
Object.prototype.toString(d);
converts Object.prototype
to string and ignores its argument. In
Object.prototype.ToString.apply(d);
d
gets passed as this
to the ToString
method (as if d.toString()
with toString
referring to Object.prototype.toString
was called), which is what the method respects.
See Function#apply
and Object#toString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With