From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/globalthis/index.html | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/globalthis/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/globalthis') diff --git a/files/zh-cn/web/javascript/reference/global_objects/globalthis/index.html b/files/zh-cn/web/javascript/reference/global_objects/globalthis/index.html new file mode 100644 index 0000000000..8e0e0feeb2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/globalthis/index.html @@ -0,0 +1,92 @@ +--- +title: globalThis +slug: Web/JavaScript/Reference/Global_Objects/globalThis +tags: + - JavaScript + - Reference + - global + - globalThis + - this + - 全局 + - 参考 + - 属性 +translation_of: Web/JavaScript/Reference/Global_Objects/globalThis +--- +
{{jsSidebar("Objects")}}
+ +

全局属性 globalThis 包含全局的 this 值,类似于全局对象(global object)。

+ +
{{EmbedInteractiveExample("pages/js/globalprops-globalthis.html","shorter")}}
+ + + +

{{JS_Property_Attributes(1, 0, 1)}}

+ +

语法

+ +
globalThis
+ +

描述

+ +

在以前,从不同的 JavaScript 环境中获取全局对象需要不同的语句。在 Web 中,可以通过 windowself 或者 frames 取到全局对象,但是在 Web Workers 中,只有 self 可以。在 Node.js 中,它们都无法获取,必须使用 global

+ +

在松散模式下,可以在函数中返回 this 来获取全局对象,但是在严格模式和模块环境下,this 会返回 undefined。 You can also use Function('return this')(), but environments that disable {{jsxref("eval", "eval()")}}, like CSP in browsers, prevent use of {{jsxref("Function")}} in this way.

+ +

globalThis 提供了一个标准的方式来获取不同环境下的全局 this  对象(也就是全局对象自身)。不像 window 或者 self 这些属性,它确保可以在有无窗口的各种环境下正常工作。所以,你可以安心的使用 globalThis,不必担心它的运行环境。为便于记忆,你只需要记住,全局作用域中的 this 就是 globalThis

+ +

HTML 与 WindowProxy

+ +

在很多引擎中, globalThis 被认为是真实的全局对象的引用,但是在浏览器中,由于 iframe 以及跨窗口安全性的考虑,它实际引用的是真实全局对象(不可以被直接访问)的 {{jsxref("Proxy")}} 代理。在通常的应用中,很少会涉及到代理与对象本身的区别,但是也需要加以注意。

+ +

命名

+ +

并没有采用一些更常见的命名方式类似 self 和 global 是因为考虑到前向兼容,为了避免影响到现存代码的正常工作。 更多相关信息可以查看 language proposal's "naming" document 。

+ +

示例

+ +

globalThis 之前,获取某个全局对象的唯一方式就是 Function('return this')(),但是这在某些情况下会违反 CSP 规则,所以,es6-shim 使用了类似如下的方式:

+ +
var getGlobal = function () {
+  if (typeof self !== 'undefined') { return self; }
+  if (typeof window !== 'undefined') { return window; }
+  if (typeof global !== 'undefined') { return global; }
+  throw new Error('unable to locate global object');
+};
+
+var globals = getGlobal();
+
+if (typeof globals.setTimeout !== 'function') {
+  // 此环境中没有 setTimeout 方法!
+}
+
+ +

但是有了 globalThis 之后,只需要:

+ +
if (typeof globalThis.setTimeout !== 'function') {
+  //  此环境中没有 setTimeout 方法!
+}
+ +

规范

+ + + + + + + + + + +
规范
{{SpecName("ESDraft", "#sec-globalthis", "globalThis")}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.globalThis")}}

+ +

实现进度

+ +

下表提供了此特性的每日实施状态,因为该功能尚未达到跨浏览器的稳定性。 通过在每个浏览器的JavaScript引擎的daily版本或最新版本中运行 Test262(JavaScript 的标准测试套件)中的相关功能测试得到了如下数据。

+ +
{{EmbedTest262ReportResultsTable("globalThis")}}
-- cgit v1.2.3-54-g00ecf