JavaScript における this と self の違い 質問する

JavaScript における this と self の違い 質問する

誰もがJavaScriptで知っていますが、実際に遭遇するthis例もあります。selfここ

では、 JavaScript のthisとの違いは何でしょうか?self

ベストアンサー1

他に設定されていない場合、の値selfwindowJavaScriptxの任意のプロパティに、 の代わりにwindowとして簡単にアクセスできます。したがって、は本当にxwindow.xselfwindow.self、これはthis

window.self === window; // true

グローバルスコープで実行され、厳密モードではない関数を使用している場合、thisデフォルトで になりwindow、したがって

function foo() {
    console.log(
        window.self === window, // is self window?
        window.self === this,   // is self this?
        this === window         // is this window?
    );
}
foo(); // true true true

異なるコンテキストで関数を使用している場合、thisはそのコンテキストを参照しますが、selfのままですwindow

// invoke foo with context {}
foo.call({}); // true false false

定義window.selfW3C 2006ワーキングドラフトウィンドウオブジェクト ここ

おすすめ記事