レッスンに戻る

セカンドバインド

importance: 5

追加のバインディングでthisを変更できますか?

出力はどうでしょうか?

function f() {
  alert(this.name);
}

f = f.bind( {name: "John"} ).bind( {name: "Ann" } );

f();

回答: John.

function f() {
  alert(this.name);
}

f = f.bind( {name: "John"} ).bind( {name: "Pete"} );

f(); // John

f.bind(...)によって返された特殊なバインドされた関数オブジェクトは、作成時のみコンテキスト (および提供された場合は引数) を記憶します。

関数を再バインドすることはできません。