-
Notifications
You must be signed in to change notification settings - Fork 0
/
global object.js
39 lines (28 loc) · 875 Bytes
/
global object.js
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
// The global object in different environments
// browser
// this === window // -> true
// this === globalThis // -> true
// node scripts
// console.log(this === module.exports); // -> true
// node terminal
// this === global // -> true
// this === globalThis // -> true
// deno scripts
// console.log(this === undefined); // -> true
// deno terminal
// this === window // -> true
// this === globalThis // -> true
// We can use these to identify the
// environment in which the script is running
const isBrowser = () => {
try { return this === window } catch { return false }
}
const isNode = () => {
try { return this === module.exports } catch { return false }
}
const isDeno = () => {
try { return this === undefined } catch { return false }
}
if (isBrowser()) console.log('Browser')
if (isNode()) console.log('Node');
if (isDeno()) console.log('Deno');