窗口对象包含什么? [英] What does the window object contain?

查看:105
本文介绍了窗口对象包含什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在全局范围内探索这个关键字,并发现该上下文中的 this 指的是窗口。

I was exploring the this keyword in the global scope, and discovered that this in that context refers to the window.

我将的值记录到控制台,并看到下面图片中显示的巨大列表。

I logged the value of this to the console, and saw a huge list shown in the image below.

我看到的列表包含什么,以及它是如何填充的?

What does the list I'm seeing contain, and how is it populated?

推荐答案

首先,读者对词汇环境的简要定义,以及它的关系全球环境,以及全球环境与全球对象的关系。

First, a brief definition for readers of what a lexical environment is, as well as how it relates to the global environment, and in turn how the global environment relates to the global object.

词汇环境包括:


  • 环境记录,用于存储在环境范围内创建的标识符绑定,

  • 对out的引用环境,

  • 对其中包含的任何环境的引用。

词法环境继承在其所包含的环境中声明的变量定义,并在每次函数声明,块语句时创建,或者评估try语句的catch子句。变量定义不能在它们定义的词汇环境之外访问。

Lexical environments inherit variable definitions declared in the environments they are contained within, and are created each time a function declaration, a block statement, or a catch clause of a try statement is evaluated. Variable definitions are not accessible outside the lexical environment they were defined in.

以下示例:


  • 使用 var 声明定义一个全局变量,初始化为一个函数表达式,它创建一个新的词法环境,

  • 在新环境中定义变量,再次使用 var 声明,这次初始化为字符串值,

  • 演示该变量在其定义的环境之外是不可访问的:

  • defines a global variable using a var declaration, initialized to a function expression, which creates a new lexical environment,
  • defines a variable in the new environment, again using a var declaration, this time initialized to a string value, and
  • demonstrates that the variable is not accessible outside of the environment it is defined in:
var hello = function() {
    var world = "fubar";
    console.log(world); // "fubar";
}

console.log(world); // ReferenceError: world is not defined






全局环境是一个词法环境,其外部环境引用为null,其中包含一个关联的全局对象,其属性提供了一些全局环境的标识符绑定,特别是排除了使用 const 声明,以及其他可能的排除。


The global environment is a lexical environment whose outer environment reference is null, and which includes an associated global object whose properties provide some of the global environment's identifier bindings, specifically excluding variables defined using let or const declarations, as well as other possible exclusions.

var hello = "world";
console.log(hello, window.hello); // "world", "world"

let foo = "bar";
console.log(foo, window.foo) // "bar", undefined






现在,在上下文中回答你的问题:


Now, to answer your question in context:


我看到的清单是什么包含,它是如何填充的?

What does the list I'm seeing contain, and how is it populated?

您看到的列表包含全局对象的属性,包括:

The list you are seeing contains the properties of the global object, which consists of:


  • 浏览器提供的预先填充的标识符绑定 - 其中一些是标准的,另一些是特定于JavaScript引擎或浏览器实现的 -

  • 全局变量由在当前页面上运行的脚本设置,或者

  • 由您可能安装的浏览器扩展设置的全局变量。

此答案中包含的信息应符合 ECMAScript 2015语言规范,其中还包含此处使用的大多数术语的定义,我强烈建议当你有兴趣轻松阅读时,你可以浏览一下这个文件。

The information contained in this answer should conform to the ECMAScript 2015 Language Specification, which also contains definitions for most of the terms used here, and I strongly encourage you to skim over that document any time you're in the mood for some light reading.

如果你找到了dis这个答案和ECMAScript语言规范之间的问题,请随时编辑这个答案以符合。

这篇关于窗口对象包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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