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 es 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?
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 es 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?
-
"[object Window]"
is returned for both in Firebug. – Chad Commented Jan 20, 2012 at 16:04 -
Both are
"[object DOMWindow]"
in Chrome 16. – Ash Burlaczenko Commented Jan 20, 2012 at 16:07 - @AshBurlaczenko ah, so maybe there was a bug somewhere that was fixed. If you can find some report of it and post as an answer I'd accept it. – Alex Turpin Commented Jan 20, 2012 at 16:08
-
@AshBurlaczenko: I'm using Chrome 16 and I get
[object Object]
and[object DOMWindow]
– qwertymk Commented Jan 20, 2012 at 16:19 - @qwertymk, heres a screenshot using Chrome 16 imageshack.us/photo/my-images/811/printscreenqw.png – Ash Burlaczenko Commented Jan 20, 2012 at 16:31
3 Answers
Reset to default 4WebKit 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
Live example shows that most are "[object Window]"
this bug mostly applies to console. Note that Object.prototype.toString.call(window)
still returns global
var s = toString;
console.log(s()); // "[object global]"
console.log(toString()); // "[object Object]"
console.log(window.toString()); // "[object Window]"
(function () {
var s = toString;
console.log(s()); // "[object Undefined]"
})();
(function () {
var s = window.toString;
console.log(s()); // "[object Window]"
})();
console.log(Object.prototype.toString.call(window)); // "[object global]"
console.log(window.toString.call(window)); // "[object DOMWindow]"
You found a bag of undefined behaviour. I remend you run away.
Part of this can be explained away by saying that the global context and the window object are probably not one and the same thing (big hint is [object global]).
Note that this === window
in all 5 cases.
Note that window.toString === Object.prototype.toString; // false
explains quite a lot of this. It seems like window.toString
is a special (but different) function
With the first one, the console tells you about the object
Object
, by which the function toString() is owned and from which everything inherits:With the second, it executes the
.toString()
function ofthis
which is in this case the window object:
Before I get a -20 for this answer, it is a deduction (so I'm not sure) based on the output [object Object]
: for me it tells that the method is executed from Object
.
If you do the same in Firebug, it actually executes from the window
object:
I guess this specific to a console's implementation... or a bug in that Chrome's version.