为什么`toString`不等同于`window.toString`? [英] Why isn't `toString` equivalent to `window.toString`?

查看:65
本文介绍了为什么`toString`不等同于`window.toString`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信所有全局变量都可以从全局对象访问.因此,如果我可以访问 x (并且 x 不在本地绑定),则 window.x 是相同的值.

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.

但是,在网页中(JSFiddle上的 ):

However, in a webpage (on JSFiddle):

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

但是在控制台中:

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

这是为什么?为什么 window 是Chrome控制台中的全局对象,但是 toString 没有绑定到 window.toString ?在Firefox的控制台中绑定到 toString 的内容是什么?控制台中还有哪些其他全局值?

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?

推荐答案

toString 不是全局变量.这是几乎所有对象(包括 window 对象)共享的一种方法.

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

实际的全局变量将始终在 window 对象上可用.

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

这篇关于为什么`toString`不等同于`window.toString`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆