javascript - Why isn't `toString` equivalent to `window.toString`? - Stack Overflow

I'd believed that all global variables were accessible from the global object. So if I can access

I'd believed that all global variables were accessible from the global object. So if I can access x (and x isn't bound locally), then window.x is the same value.

However, in a webpage (on JSFiddle):

window === this // true in Chrome and Firefox
toString === window.toString // true in Chrome and Firefox

But in the console:

window === this // true in Chrome console and Firebug, false in Firefox web console
toString === window.toString // false in Chrome, Firebug and Firefox web console

Why is this? Why is window the global object in Chrome's console but toString not bound to window.toString? What is toString bound to in Firefox's console? What other global values are different in the console?

I'd believed that all global variables were accessible from the global object. So if I can access x (and x isn't bound locally), then window.x is the same value.

However, in a webpage (on JSFiddle):

window === this // true in Chrome and Firefox
toString === window.toString // true in Chrome and Firefox

But in the console:

window === this // true in Chrome console and Firebug, false in Firefox web console
toString === window.toString // false in Chrome, Firebug and Firefox web console

Why is this? Why is window the global object in Chrome's console but toString not bound to window.toString? What is toString bound to in Firefox's console? What other global values are different in the console?

Share Improve this question edited Feb 25, 2013 at 16:25 Wilfred Hughes asked Jan 4, 2013 at 15:42 Wilfred HughesWilfred Hughes 31.2k15 gold badges148 silver badges201 bronze badges 4
  • I cannot reproduce. What am I missing? – Travis Watson Commented Jan 4, 2013 at 16:36
  • See ments on my response below, apparently you're seeing a Firebug issue. – Travis Watson Commented Jan 4, 2013 at 16:50
  • @Travis I've changed the example to avoid confusion with the Firebug issue. – Wilfred Hughes Commented Feb 25, 2013 at 16:25
  • Try toString === console.toString while in the console. – Travis Watson Commented Feb 25, 2013 at 18:30
Add a ment  | 

3 Answers 3

Reset to default 3

toString is not a global variable. It's a method shared by almost all objects, including the window object.

An actual global variable would always be available on the window object.

perhaps this is related to this question? It's all related to the context, I believe

toString.call("foo") == this.toString.call("foo")

but

tostring.call("foot") != window.toString.call("foo") when this != window

I cannot reproduce your claim in Firefox. They're both returning [xpconnect wrapped native prototype].

To help clear this up: everything available globally IS available via the global object. However, there could be properties available through the global object that are not necessarily available globally. This is due to the prototypal inheritance pattern in Javascript and the lack of specification on how this situation should be handled.

So, should an interpreter attempt to resolve global lookups via prototypal inheritance down the global object chain? Does the global object inherit from anything else? I think the various Javascript interpreters are inconsistent here, but someone more familiar with ECMAScript specifications could weigh in.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744383062a4571526.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信