Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly more horrifying, but more universal technique #3

Open
mathiasbynens opened this issue Apr 18, 2019 · 2 comments
Open

Slightly more horrifying, but more universal technique #3

mathiasbynens opened this issue Apr 18, 2019 · 2 comments

Comments

@mathiasbynens
Copy link
Contributor

A horrifying globalThis polyfill in universal JavaScript describes a technique that could be useful for this project.

Instead of the current approach of detecting known environment-specific references to the global this, the new technique doesn't depend on any such bindings, and is 100% universal JavaScript (although you would still need a single fallback if IE <= 10 support is a concern).

@Richienb
Copy link

Modern implementation:

module.exports = () => {
	if (typeof globalThis === "object") {
		return globalThis
	}

	Object.defineProperty(Object.prototype, '__magic__', {
		get: function() {
			return this;
		},
		configurable: true
	});

	const result = __magic__;
	delete Object.prototype.__magic__;

	return result
}

@ljharb
Copy link
Member

ljharb commented Aug 27, 2020

I see that the full approach has a fallback for window in IE < 11. Are there any non-browser engines that fail to work in the Object.defineProperty case?

If we have to rely on both window and Object.prototype anyways, i'm not sure if this (quite clever and indeed horrifying) approach is an improvement over the current implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants