question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

global is undefined in a strict environment

See original GitHub issue

The following code will be generated when using the es6-set polyfill in a Rollup IIFE build:

(function() {
  "use strict";

  var isImplemented = function() {
    // ...
  };
  /* eslint strict: "off" */
  var global = (function() {
    return this;
  })();

  if (!isImplemented()) {
    Object.defineProperty(global, "Set", {
      value: polyfill$1,
      configurable: true,
      enumerable: false,
      writable: true
    });
  }
})();

global will be undefined because of the "use strict";, and the error Object.defineProperty: argument is not an Object will be thrown.

Is the recommended solution to remove the "use strict";, or could perhaps the global implementation be changed to something along the lines of the implementation in core-js?

// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
var global =
  typeof window == "object" && window && window.Math == Math
    ? window
    : typeof self == "object" && self && self.Math == Math
    ? self
    : Function("return this")();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
zloirockcommented, Apr 28, 2019

@medikoo CSP is not an issue since in engines where it’s possible we check window and self before usage of Function constructor.

2reactions
mathiasbynenscommented, Apr 26, 2019

I’ll just leave this here: https://mathiasbynens.be/notes/globalthis

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting a reference to the global object in an unknown ...
This technique will work for any host environment, but not in strict mode, because an undefined this does not reference the global object...
Read more >
Global app variables under "use strict" · Issue #1380 - GitHub
It seems it is impossible to define global app variables when running code under "use strict". In documentation it is written:.
Read more >
Detecting Undefined Variables - lua-users wiki
The reason is that if a variable is not recognized by Lua as a local variable (e.g. by static declaration of the variable...
Read more >
globalThis - JavaScript - MDN Web Docs
The this keyword could be used inside functions running in non–strict mode, but this will be undefined in modules and inside functions running ......
Read more >
What is globalThis, and why should you start using it?
The globalThis proposal, now in stage 4, aims to consolidate the increasingly fragmented ways of accessing the global object in JavaScript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found