const human = { name: 'Alex', see: () => { console.log('See you ' + this.name); }, hi: function (greeting) { console.log(greeting + ' ' + this.name); return greeting + ' ' + this.name; }, hi2s() { setTimeout(this.hi.bind(this, 'hi'), 2000); } } human.hi2s();
課題 ・1秒後に、Hi Alex と出力する方法(bind)
setTimeout(person.hi.bind(human, 'hi'), 1000);
bind(参照したいオブジェクト, そのオブジェクトの引数にいれたい値)
bindの第一引数は、参照したいオブジェクト名を書いて、第二引数は、human.hi の hiメソッド の引数を取る。
・alert alertがとる引数は、文字列
・this アロー関数は、thisを取らない
hi2s() { setTimeout(this.hi.bind(this, 'hi'), 2000); }
上記の this は、humanを指す