→
グローバル オブジェクトと同等であるグローバルな this が格納されています。
globalThis のプロパティ属性:
書込可能 | 可 |
---|---|
列挙可能 | 不可 |
設定可能 | 可 |
globalThis 以前は、その環境のグローバル オブジェクトを取得する信頼性の高い方法は Function('return this')() だけでした。ただし、これは設定によっては CSP(Content Security Policy)違反が発生するため、es6-shim では次のようなチェックを使用します:
var getGlobal = function () { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } throw new Error('unable to locate global object'); }; var globals = getGlobal(); if (typeof globals.setTimeout !== 'function') { // no setTimeout in this environment! }
globalThis が利用可能になったため、異なる環境間でグローバルを追加で検索する必要がなくなりました:
if (typeof globalThis.setTimeout !== 'function') { // no setTimeout in this environment! }