Created
December 14, 2013 15:14
-
-
Save rauschma/7960405 to your computer and use it in GitHub Desktop.
Cross-platform way of referring to the global object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (glob) { | |
// glob points to global object | |
}(typeof window !== 'undefined' ? window : global)); |
Absolutely! Language shims may be another use case, but also a rare one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
True (and that’s covered by my second solution) but when do you need a reference to the real
global
object in Node.js modules? Not very often. Most of the time you just want to assign something tomodule.exports
so that it can be used outside of the module.That said, here is an example of an exception to this, where a
CSS
property is added to theglobal
object: https://github.com/mathiasbynens/CSS.escape/blob/master/css.escape.js But other than weird DOM polyfills like this I don’t think there’s a real use case for getting the trueglobal
object in a Node.js module.