LoginSignup
1
0

More than 5 years have passed since last update.

JavaScriptのbindのメモ

Posted at
class Person {
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log(this.name + "さん");
  }
}

person = new Person('Joji');
person.greet();
//Jojiさん


let person2 = person.greet;
person2()
//Uncaught TypeError: Cannot read property 'name' of undefined

let person3 = person.greet.bind(person);
person3()
//Jojiさん

bind()の第1引数をthis値にバインドしてくれる。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0