4 ms·
The line: __magic__.globalThis = __magic__; // lolwat Seems needlessly, well, magic. Keeping it at two lines would make it easier to grok: Object.proto
by underwater 7y ago
The line:
__magic__.globalThis = __magic__; // lolwat
Seems needlessly, well, magic. Keeping it at two lines would make it easier to grok:
Object.prototype.__defineGetter__('__getValueOfThis__', function() {
return this;
});
const globalThisValue = __getValueOfThis__; // Invoke getter on "global object"
globalThisValue.globalThis = globalThisValue;
Just because it magical doesn't mean it has to be confusing.
- chrismorgan 7y agoNow you’ve added a variable declaration, which makes life more painful for keeping your polyfill small. The name of the game in these things is minimising size—though I will allow that a more verbose form can be useful for teaching.
- thrower123 7y agoWorrying about keeping polyfills micro-optimized seems somewhat silly, when a typical website is pulling in megabytes of other scripts and trackers. Not to mention when just the headers and cookies on every request can balloon up into the tens or hundreds of kilobytes.
- lucideer 7y agoLeaving it at one line, but just naming it something more explanatory than `__magic__` would also improve it slightly (without needing an extra var declaration). `__getValueOfThis__` is good. Something like `__globalThisGetter__` or `__globalThisPolyfillGetter__` might be even clearer given the deletion line. e.g.: __globalThisPolyfillGetter__.globalThis = __globalThisPolyfillGetter__; delete Object.prototype.__globalThisPolyfillGetter__;
- IgorPartola 7y ago__getGlobal__()
- lucideer 7y agoMaybe. But as the article discusses, globalThis !== global