Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do toString() and this.toString() produce different results in Chrome's console?

Now this has no practical implications at all, but I am curious about this little quirk I stumbled upon.

Basically, in Chrome's developer console, this

toString()

returns [object Object], whereas this

this.toString()

returns [object DOMWindow].

As far as I know, this only happens from the console, as can be seen on this jsFiddle. Someone on ##javascript found this link explaining where the function comes from. However, it doesn't explain the discrepancy there is in the behavior when used within or outside the console.

So why do toString() and this.toString() produce different results in Chrome's console?

like image 377
Alex Turpin Avatar asked Jan 20 '12 16:01

Alex Turpin


People also ask

What does toString return in JavaScript?

The toString() method returns a string representing the source code of the specified Function .

How many parameters are required for toString in JavaScript?

Parameters Used: This method accepts a single optional parameter base. This parameter specifies the base in which the integer is represented in the string. It is an integer between 2 and 36 which is used to specify the base for representing numeric values.

Do all JavaScript objects have toString?

Using toString() to detect object classtoString() can be used with every object and (by default) allows you to get its class.


1 Answers

WebKit happened to use wrong context for global calls in the console.

(Chrome 14):

> this
DOMWindow
> this.toString()
"[object DOMWindow]"
> toString()
"[object Object]"
> valueOf()
CommandLineAPI

I think this has been fixed over here

like image 175
georg Avatar answered Sep 22 '22 11:09

georg