tc39 / proposal-global

ECMAScript Proposal, specs, and reference implementation for `global`
http://tc39.github.io/proposal-global/
MIT License
349 stars 18 forks source link

Polyfill does not work in an ES module context #43

Closed rictic closed 5 years ago

rictic commented 5 years ago

When running as an ES Module, the global this is always undefined.

rictic commented 5 years ago

The best version I've got:

var polyfilledGlobalThis = 
  this !== undefined ? this :
  typeof window === 'object' ? window : 
  typeof global === 'object' ? global : 
  undefined /* or maybe throw? */;
ljharb commented 5 years ago

Function(‘return this’)() absolutely works in an ES Module context.

Ether way, proposal repo polyfills aren’t for production use. Use https://npmjs.com/globalthis