You need to define a module that exports each of them - window
, document
and undefined
.
So, in a window.js
file, you do:
define(function(){
// that's the actual `window` global object
return window;
});
Then, you reference it, like you would do with any other module:
define(['jQuery', 'window'], function($, window) {
// ...
});
Do same to cover all others you need.
However, I wouldn't recommend you to continue applying this approach. There were 2 general reasons why this approach was valuable.
It's a hack for a better compression rate. If undefined
is a variable in the function scope rather than a global object property, minifies can reduce it to a single letter (thus achieving a better compression rate).
However, that's valid if you only use minification only. If you use minification + gzip to serve your production scripts (like you should!), the approach is irrelevant. Gzip uses the LZ77 algorithm that replaces repeated occurrences of data with references. So it will catch & compress all window
or document
or undefined
or any other related occurrences and you're safe.
Before ES5*, undefined
was a property of the global object without write-protection. So it could be overwritten, resulting in strange and unexpected behavior.
However, with ES5, the global properties undefined
, NaN
and Infinity
became read-only. So with its general adoption in all modern browsers - of course with the exception of IE 9 - overwriting those values was not possible anymore.
Plus, with ES6 we have the Proxy. If we suspense that someone is modifying a global variable, you can use a proxy to debug (track) any and every property access or set event, with a handler.get
or handler.set
trap. Read more about Proxies here.