最終更新:2022-12-16 (金) 02:37:54 (496d)  

Function.prototype.bind
Top / Function.prototype.bind

Requires JavaScript 1.8.5

新たな関数(束縛された関数 = a bound function)を生成して返します

bind(thisArg)
bind(thisArg, arg1)
bind(thisArg, arg1, arg2)
bind(thisArg, arg1, ... , argN)

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

引数

thisArg

  • funcにthisとして渡される値

arg1...

  • func を呼び出す時、バインドされた関数に与えられる引数の前に付けて渡す引数。
    • (引数は自動的に転送される)

  • function say(message) {
        return `${message} ${this.fullName}!`;
    }
    const person = {
        fullName: "Brendan Eich"
    };
    // `this`を`person`に束縛した`say`関数をラップした関数を作る
    const sayPerson = say.bind(person, "こんにちは");
    sayPerson(); // => "こんにちは Brendan Eich!"

関連