Why is the value of `toString` equivalent to` window.toString`?

I believed that all global variables are accessible from a global object. Therefore, if I can access x (and x not bound locally), then window.x is the same value.

However, on the web page ( 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 global object in the Chrome console, but toString not bound to window.toString ? What is toString related to the Firefox console? What other global values ​​differ in the console?

+6
source share
3 answers

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

A valid global variable will always be available for the window object.

+3
source

perhaps this is due to this issue ? All this is context related, I suppose

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

but

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

I cannot reproduce your claims in Firefox. They return [xpconnect wrapped native prototype] .

To fix this problem: all available are globally accessible through the global object. However, there may be properties accessible through a global object that are not necessarily available worldwide. This is due to the prototype inheritance pattern in Javascript and the lack of a specification of how this situation should be handled.

So, should the interpreter try to allow global search through prototype inheritance across a global chain of objects? Does the global object inherit anything else? I think the various Javascript interpreters are not compatible here, but someone more familiar with the ECMAScript specifications might weigh it.

+1
source

All Articles