this === undefined when polyfills are wrapped in an IIFE containing `use strict`
See original GitHub issueExplanation
(function () {
'use strict';
(function (global) {
function Promise () {
// ...
}
global.Promise = Promise; // Uncaught TypeError: Cannot set property 'Promise' of undefined
}(this));
}())
Problem files
➜ git grep --name-only '[}|)](this)' -- polyfills/*polyfill.js
polyfills/Event.hashchange/polyfill.js
polyfills/JSON/polyfill.js
polyfills/Navigator.prototype.geolocation/polyfill.js
polyfills/Promise/polyfill.js
polyfills/URL/polyfill.js
polyfills/atob/polyfill.js
polyfills/devicePixelRatio/polyfill.js
polyfills/document.querySelector/polyfill.js
polyfills/getComputedStyle/polyfill.js
polyfills/localStorage/polyfill.js
polyfills/matchMedia/polyfill.js
polyfills/performance.now/polyfill.js
polyfills/requestAnimationFrame/polyfill.js
polyfills/viewport/polyfill.js
15 polyfills does not work as expected
Issue Analytics
- State:
- Created 9 years ago
- Comments:20 (20 by maintainers)
Top Results From Across the Web
Is it a good practice to wrap "strict mode" inside an IIFE (self ...
Is there any basis as to why should use strict; be kept inside IIFE, or is this just a "baseless objection" raised with...
Read more >The 10 Most Common JavaScript Issues Developers Face
Prevents accidental globals. Without strict mode, assigning a value to an undeclared variable automatically creates a global variable with that name. This is ......
Read more >JavaScript trivia questions in front end interviews | Front End ...
If in strict mode ( 'use strict' ), this will be undefined instead of the global ... with function are considered to be...
Read more >Advanced Javascript
undefined is used by javascript to indicate that a variable is a missing param, unknown property, uninitialised. null is more likely? to be...
Read more >Converse.js API Documentation Source: headless/dist ...
"If data contains a code point that is not one of // // U+002B (+) // U+002F ... function (punycode, IPv6, SLD, root)...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

OK, how about:
I’ve tested this and it seems to work. window will not throw a referenceerror if used with typeof, and then inside the IIFE we can redefine
windowandselfto be equal tothis. That gives us the most flexible options for supporting third party polyfill code which might be assigning into properties ofwindow,selforthis, it’s compatible with web workers, while still being able to cope with environments in which the developer has redefinedselforthis.I believe the justification for the use of the
}(this));pattern is that the polyfills may be loaded in a worker rather than a window. We don’t want to stop the polyfills from working in a web worker. I agree with Juan, if the polyfill wanted to set use strict, it could do so, but the reason use strict works in the way it does is to avoid breaking the web for code that already exists and would stop working if the stricter rules of use strict were applied.I don’t have any objection to
Function('return this')()if that works in everything above our baseline, however, you usedPromiseas your example use case, and that’s interesting because we are simply copying the source of the promise polyfill from the third party repo, which uses the}(this));pattern, so we don’t have control over the use of the pattern there.Potentially the easiest and cleanest solution would be to add a definition of
thisat the top of the closure that we output when we generate the polyfill bundle:This will solve your problem provided that you generate your autopolyfiller bundle by using the service API rather than simply reading the polyfill source files individually. That would probably be a good idea anyway, since it would enable you to easily output a polyfill service CDN URL from autopolyfiller rather than a one-size-fits-all bundle.