@@ -41,17 +41,56 @@ package object special {
41
41
def delete (obj : scala.Any , key : scala.Any ): Unit =
42
42
throw new java.lang.Error (" stub" )
43
43
44
- /** The value of the global JavaScript `this`.
44
+ /** The value of the JavaScript `this` at the top-level of the generated
45
+ * file.
45
46
*
46
47
* This returns the value that would be obtained by writing `this` at the
47
- * top-level of the JavaScript file generated by Scala.js. In most
48
- * JavaScript environments, this is equivalent to the *global object*, but
49
- * it is not necessarily the case. For example, on Node.js, `this` is the
50
- * object representing the `exports` of the current module.
48
+ * top-level of the JavaScript file generated by Scala.js. In scripts, this
49
+ * is equivalent to the *global object*, but in other environments, it is
50
+ * not necessarily the case. For example, in CommonJS modules on Node.js,
51
+ * `this` is the object representing the `exports` of the current module,
52
+ * and it is `undefined` in ECMAScript modules.
51
53
*
52
54
* Using this value should be rare, and mostly limited to writing code
53
55
* detecting what the global object is. For example, a typical detection
54
- * code looks like:
56
+ * code--in case we do not need to worry of ES modules--looks like:
57
+ * {{{
58
+ * val globalObject = {
59
+ * import js.Dynamic.{global => g}
60
+ * if (js.typeOf(g.global) != "undefined" && (g.global.Object eq g.Object)) {
61
+ * // Node.js environment detected
62
+ * g.global
63
+ * } else {
64
+ * // In all other well-known environment, we can use the global `this`
65
+ * js.special.fileLevelThis
66
+ * }
67
+ * }
68
+ * }}}
69
+ * Note that the above code is not comprehensive, as there can be JavaScript
70
+ * environments where the global object cannot be fetched neither through
71
+ * `global` nor `this`. If your code needs to run in such an environment, it
72
+ * is up to you to use an appropriate detection procedure.
73
+ */
74
+ @ inline
75
+ def fileLevelThis : scala.Any =
76
+ scala.scalajs.runtime.linkingInfo.globalThis
77
+
78
+ /** The value of the JavaScript `this` at the top-level of the generated
79
+ * file.
80
+ *
81
+ * *Deprecated*: this value does not correspond to JavaScript's
82
+ * `globalThis` value. Use `fileLevelThis` instead.
83
+ *
84
+ * This returns the value that would be obtained by writing `this` at the
85
+ * top-level of the JavaScript file generated by Scala.js. In scripts, this
86
+ * is equivalent to the *global object*, but in other environments, it is
87
+ * not necessarily the case. For example, in CommonJS modules on Node.js,
88
+ * `this` is the object representing the `exports` of the current module,
89
+ * and it is `undefined` in ECMAScript modules.
90
+ *
91
+ * Using this value should be rare, and mostly limited to writing code
92
+ * detecting what the global object is. For example, a typical detection
93
+ * code--in case we do not need to worry of ES modules--looks like:
55
94
* {{{
56
95
* val globalObject = {
57
96
* import js.Dynamic.{global => g}
@@ -69,9 +108,13 @@ package object special {
69
108
* `global` nor `this`. If your code needs to run in such an environment, it
70
109
* is up to you to use an appropriate detection procedure.
71
110
*/
111
+ @ deprecated(
112
+ " Does not correspond to JavaScript's globalThis. " +
113
+ " Use fileLevelThis instead." ,
114
+ " 0.6.29" )
72
115
@ inline
73
116
def globalThis : scala.Any =
74
- scala.scalajs.runtime.linkingInfo.globalThis
117
+ fileLevelThis
75
118
76
119
/** Exact equivalent of the `debugger` keyword of JavaScript.
77
120
*
0 commit comments