最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

Share Improve this question edited Sep 5, 2018 at 7:57 Jacob 6251 gold badge8 silver badges16 bronze badges asked Jan 20, 2012 at 16:01 Alex TurpinAlex Turpin 47.8k23 gold badges116 silver badges146 bronze badges 6
  • "[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
 |  Show 1 more ment

3 Answers 3

Reset to default 4

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

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

  1. With the first one, the console tells you about the object Object, by which the function toString() is owned and from which everything inherits:

  2. With the second, it executes the .toString() function of this 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.

发布评论

评论列表(0)

  1. 暂无评论