with-global
Run code that depends on presence of a global variable without permanent pollution of the global namespace.
Useful with browserify/webpack.
Example
Say you have an old jquery plugin that never heard of UMD
and wants the $
global, but you don't want to override
window.$
(maybe some other code depends on another version
of jquery).
Solution:
- Set required globals
- Evaluate the nasty script
- Restore old globals
const $ = ;const withGlobals = ; ; // window.$ and window.jQuery were restored to their previous value
Note that there is still one thing the nasty module has to do right:
that is to capture the values of necessary globals like this
(function ($) { ... })($);
Most of the time they do do this.