-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-7.html
119 lines (115 loc) · 4.66 KB
/
14-7.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2 id="14-7">14-7 絕對全域物件 globalThis</h2>
<p>全域屬性 globalThis 包含全域的 this 值,類似於全域物件(global object)。</p>
<pre><code class="language-js">
function canMakeHTTPRequest() {
return typeof globalThis.XMLHttpRequest === 'function';
}
console.log(canMakeHTTPRequest());
// Expected output (in a browser): true
</code></pre>
<h3>說明</h3>
<p>
在以前,從不同的 JavaScript 環境中獲取全域物件需要不同的語句。在 Web 中,可以通過
window、self 或者 frames 取到全域物件,但是在 Web Workers 中,只有 self 可以。在Node.js
中,它們都無法獲取,必須使用 global。
</p>
<p>
在一般模式下,可以在函數中回傳 this 來獲取全域物件,但是在嚴格模式和模組環境下,this
會回傳 undefined。你也可以使用 Function('return
this')(),但那些禁用eval()的環境,如在瀏覽器中的內容安全策略(Content-Security-Policy,
CSP),不允許這樣使用Function。
</p>
<p>
globalThis 提供了一個標準的方式來獲取不同環境下的全域 this
物件(也就是全域物件自身)。不像 window 或者 self
這些屬性,它確保可以在有無窗口的各種環境下正常工作。所以,你可以安心的使用
globalThis,不必擔心它的運行環境。為便於記憶,你只需要記住,全域作用域中的 this 就是
globalThis。
</p>
<p>
在 globalThis 之前,獲取某個全域物件的唯一方式就是 Function('return
this')(),但是這在某些情況下會違反 CSP 規則,所以, es6-shim 使用了類似如下的方式:
</p>
<h3>HTML 與 WindowProxy</h3>
<p>
在很多引擎中, globalThis 被認為是真實的全域物件的引用,但是在瀏覽器中,由於 iframe
以及跨窗口安全性的考慮,它實際引用的是真實全域物件(不可以被直接訪問)的 Proxy
代理。在通常的應用中,很少會涉及到代理與物件本身的區別,但是也需要加以注意。
</p>
<p></p>
<pre><code class="language-js">
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 方法!
}
</code></pre>
<p>但是有了 globalThis 之後,只需要:</p>
<pre><code class="language-js">
if (typeof globalThis.setTimeout !== "function") {
// 此環境中沒有 setTimeout 方法!
}
</code></pre>
<p>資料來源:</p>
<p>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis"
>
https://developer.mozilla.org/.../globalThis
</a>
</p>
<p>
<a href="https://www.youtube.com/watch?v=_VvUXvTQUTE">
https://www.youtube.com/watch?v=_VvUXvTQUTE
</a>
</p>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>