mozilla 的术语“全局对象”让我困惑

发布于 2024-09-27 23:10:59 字数 542 浏览 3 评论 0原文

有几个朋友建议我阅读 developer.mozilla.org/en/JavaScript/Reference/Global_Objects:

此处的术语“全局对象”[1] 不应与全局对象[2]混淆。这里,全局对象[3]指的是全局范围内的对象。全局对象[4]本身可以在全局范围内通过this访问。事实上,全局范围全局对象[5]的属性组成(包括继承的属性,如果有的话)。

老实说,我对上面的话完全困惑了。第一句话告诉我不要混淆,但它确实让我困惑。嗯,英语不是我的母语,也许这就是原因。 出现了 5 次全局对象和 3 次全局范围

全局对象[4,5]是指全局对象[1]还是全局对象[2]?

Several friends suggest me to read developer.mozilla.org/en/JavaScript/Reference/Global_Objects:

The term "global objects"[1] here is not to be confused with the global object[2]. Here, global objects[3] refer to objects in the global scope. The global object[4] itself can be accessed by this in the global scope. In fact, the global scope consists of the properties of the global object[5] (including inherited properties, if any).

Honestly, I am completely confused by above words. The first sentence tells me not to be confused but it does confuse me. Well, English is not my mother language, maybe this is the reason.
There appear 5 times of global object(s) and 3 times global scope!

Does global object[4,5] mean global objects[1] or global object[2]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离鸿 2024-10-04 23:10:59

Mozilla 的措辞毫无帮助。我建议你忽略它。

有一个全局对象。这是 ECMAScript 语言规范 中定义明确的构造。它有几个目的,其中最重要的是全局对象的属性在任何地方都可用。这就是 Mozilla 所说的“全局对象”;它们更准确、更有用地称为“全局对象的属性”。

本文所说的全局作用域是指不在任何函数内部的代码。在这样的代码中,this 是对全局对象的引用。在浏览器内的 JavaScript 中,window 可以被认为是全局对象并且可以在任何地方访问。

综上所述,2、4、5是同一个东西。 1 和 3 对于“全局对象的属性”来说是一个糟糕的名称,您应该忘记它。

Mozilla's wording is unhelpful. I suggest you ignore it.

There is one global object. This is a well-defined construct within the ECMAScript language specification. It has several purposes, not least of which is that properties of the global object are available everywhere. This is what Mozilla means by "Global Objects"; they're more accurately and helpfully called "properties of the global object".

What the article means by global scope is code that is not inside any function. In such code, this is a reference to the global object. In JavaScript within browsers, window can be thought of as being the global object and is accessible everywhere.

In summary, 2, 4 and 5 are the same thing. 1 and 3 are a poor name for "properties of the global object" that you should forget about.

南风几经秋 2024-10-04 23:10:59

简短回答: 45 相当于 2

长答案:“全局对象”是 JavaScript 的一种语言构造,可以使用代码中的 globalwindow 变量进行访问。该对象包含以某种方式定义的一切

“全局对象”是指在 JavaScript 环境的主范围内定义的所有对象。主作用域(或“全局作用域”)是指“全局对象”的直接子对象(属性)。

如果我在任何函数之外的 JavaScript 中定义变量 myVariable,则该变量位于“全局范围”中,即可以使用 global.myVariable (或 window. myVariable),其中 global 是对“全局对象”的引用。

我建议看看 JavaScript 的函数作用域是如何工作的: https://developer.mozilla.org /en/JavaScript/Reference/Functions_and_function_scope

Short answer: 4 and 5 are equivalent to 2.

Long answer: "global object" is a language construct of JavaScript and can be accessed using the global or window variables in your code. This object contains everything that is somehow defined.

"global objects" means all objects that are defined in the main scope of a JavaScript environment. The main scope (or "global scope") means immediate children (properties) of the "global object".

If i define a variable myVariable in JavaScript outside of any function, this variable is in the "global scope" i.e. accessible by using global.myVariable (or window.myVariable) where global is a reference to the "global object".

I recommend taking a look at how JavaScript's function scope works: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

日记撕了你也走了 2024-10-04 23:10:59

全局对象是主要对象。把它想象成上帝。就像上帝一样,只有一个全局对象。在浏览器内部,window 标识符引用它。

全局对象是所有属于上帝属性的对象(全局对象)。浏览器内置了许多全局对象,例如 locationdocumentXMLHttpRequestalert 方法、 您可以通过以下

方式创建其他全局对象:

  • 声明全局(非原始)变量(在全局执行上下文中声明变量)
  • 声明全局函数(在全局执行上下文中声明函数)

最好将全局对象称为“全局变量”、“全局函数”、“全局成员”、“全局属性”等,以免与 Global 对象混淆。

The Global object is the main object. Think of it as God. Like God, there is only one Global object. Inside the browsers, the window identifier refers to it.

Global objects are all the objects that are properties of God (the Global object). The browsers have lots of global objects built-in, like location, document, XMLHttpRequest, the alert method, etc.

You can create additional global objects by:

  • declaring global (non-primitive) variables (declaring variables in the global execution context)
  • declaring global functions (declaring functions in the global execution context)

It is probably best to refer to global objects as "global variables", "global functions", "global members", "global properties", etc. so that they are not confused with the Global object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文