- グローバルコンテキストで、関数が直接呼び出される場合
this
は、関数のコンテキスト内のグローバルオブジェクト - を指し、それは再びグローバルオブジェクト
を指ししかし、以下:
var globalThis = this;
function a() {
console.log(typeof this);
console.log(typeof globalThis);
console.log('is this the global object? '+(globalThis===this));
}
a();
ファイル
foo.js
に配置された210
は...生成します。私たちはモジュールで書くものは何でもコード
のNode.jsで$ nodejs foo.js
object
object
is this the global object? false
てみは 'はconsole.log(this)を追加するように、実際のグローバルオブジェクトを参照するために、
global
objectを使用することができます。 console.log(globalThis); '実際に何が表示されているか – mplungjan