Global Object
On this page
JavaScript Global Object (JavaScript 全局 对象 )
An object that constantly exists in the global scope is called a global object. In JavaScript, the global object supplies variables and functions that can be available everywhere. (在 全局 范围 中不断存在的 对象 被称为 a global object。 在 JavaScript中, 全局 对象 提供 变量 和 函数 可以 随时随地 。)
It is called a window in the browser, global - in Node.js, and can have other names in different environments. (它在 浏览器中被称为 a 窗口 ,在 Node.js中被称为 - , 和 can have other 不同 环境中的名称。)
Lately, a standardized name globalThis was added for the global object. It is meant to be supported by all the environments. (最近,为 全局 对象添加了 标准化 名称 globalThis It is 表示 to be supported by all the environments. )
It is possible to access all the properties of the global object directly, as shown below:
console.log("Welcome to w3cdoc");
// is the same as
window.console.log("Welcome to w3cdoc");
In a browser, global variables and functions that are declared using var (and not const or let), grow into the global object property like this:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<h1>Global object</h1>
<script>
var globalVar = 10;
alert(window.globalVar); //10 became a property of the global object
</script>
</body>
</html>
But note that modern scripts apply JavaScript modules in which things like that never happen. (但是 注意 现代 脚本 在 中应用 JavaScript 模块 哪些 类似 的事情 永远不会发生。)
A thing like that wouldn’t happen, if let was used instead of var:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<h1> Global object</h1>
<script>
let globalVar = 10;
alert(window.globalVar); //undefined, became a property of the global object
</script>
</body>
</html>
In case a value is very essential, and you want it to be globally available, you should write it as a property directly like this:
//make the current book information global so that all scripts can access it
window.currentBook = {
name: "Javascript"
};
// somewhere else in the code
console.log(currentBook.name); // Javascript
//or if we have a local variable called "currentBook", get it from window explicitly
console.log(window.currentBook.name); //Javascript
So, generally, it is discouraged to use global objects. (因此,一般来说, it 不鼓励 使用 全局 对象。)
Applying Polyfills
Applying Polyfills (应用 Polyfills )
As a rule, the global object is implemented to test the support of modern language peculiarities. (作为 a 规则, 全局 对象 已实现 以 测试 现代 的 支持 language peculiarities. )
For example, let’s check whether there exists a built-in Promise object:
if (!window.Promise) {
console.log("Your browser is old");
}
Actually, it doesn’t exist in old browsers. So if you find out there is none in your browser, then you can make polyfills. In other words, you can insert functions that are not supported by the environment, but the modern standard supports them. (实际上, it 在 旧 浏览器中不存在 。 So if you find out there is none in your 浏览器, 然后 您 可以 制作 polyfills。在 其他 单词中, 您 可以 插入 功能 不受 环境 的支持, 但 现代 标准 支持 它们。)
So, in such cases, you can act as follows:
if (!window.Promise) {
window.Promise = ... // custom implementation of the function of the modern language
}
Summary
Summary (Summary )
In short we can state that the global object includes variables that are supported anywhere. It involves JavaScript built-ins like Array and environment-specific values. (在 short we can 状态 the global object 包括 变量 , 在任何地方都支持 。 It 涉及 JavaScript 内置插件 ,如 Array 和 环境特定 值。)
A generic globalThis name is used for global objects. It is required to store values inside a global object only if they are really global for the project. Moreover, their number should be kept at a minimum. ( 通用 全局 此 名称 用于 全局 对象。 It is required to store values inside a global object only if they are really global for the project. 此外, 他们的 号码 应该保持为 a 最小。)