globalThis

Tags
javascript
Created
Apr 22, 2019 4:03 AM

https://exploringjs.com/deep-js/ch_global-scope.html

globalThis 并不直接指向全局,而是间接的 // 指向内部 WindowProxy

// 全局对象是一个设计错误

https://mathiasbynens.be/notes/globalthis

image

TL;DR globalThis is not “the global object”; it’s simply the this from the global scope.

Note: The ECMAScript spec doesn’t actually mandate that the global this inherit from Object.prototype, only that it must be an object. Object.create(null) creates an object that doesn’t inherit from Object.prototype. A JavaScript engine could use such an object as the global this without violating the spec, in which case the above code snippet still wouldn’t work (and indeed, Internet Explorer 7 did something like that!). Luckily, more modern JavaScript engines all seem to agree that the global this must have Object.prototype in its prototype chain.

从全局对象的属性上返回 this 就是全局对象。

SuperMade with Super