LoginSignup
4
4

More than 5 years have passed since last update.

ES2015 classメソッド内のthisを自インスタンスとして扱わせる。

Last updated at Posted at 2016-09-11

JavaScriptのthisの扱いは少し注意が必要です。

参考記事: JavaScriptの「this」は「4種類」??

ES2015のclassでもそれは同じこと。
下記のように呼びだされ方で結果が変わります。

index.js
'use strict';

class Message {
  constructor(message) {
    this.message = message;
  }
  show() {
    try {
      console.log(this.message);
    } catch (e) {
      console.error(e);
    }
  }
}

var m = new Message('Hello!');

m.show();
// Hello!

var show = m.show;
show();
// [TypeError: Cannot read property 'message' of undefined]

setTimeout(m.show, 0);
// undefined

thisを自インスタンスとして扱わせるには下記のような方法があります。

bindを使用

classメソッドも所詮はfunctionなのでbindメソッドでthisを明示できます。
http://stackoverflow.com/questions/31362292/can-i-use-es6-arrow-in-class-methods

index.js
'use strict';

class Message {
  constructor(message) {
    this.message = message;
    this.show = this.show.bind(this);
  }
  show() {
    try {
      console.log(this.message);
    } catch (e) {
      console.error(e);
    }
  }
}

var m = new Message('Hello!');

m.show();
// Hello!

var show = m.show;
show();
// Hello!

setTimeout(m.show, 0);
// Hello!

constructor内でarrow functionを使用

arrow functionがthisの参照先を変えないことを利用してconstructorでメソッドを定義します。

index.js
'use strict';

class Message {
  constructor(message) {
    this.message = message;
    this.show = () => {
      try {
        console.log(this.message);
      } catch (e) {
        console.error(e);
      }
    };
  }
}

var m = new Message('Hello!');

m.show();
// Hello!

var show = m.show;
show();
// Hello!

setTimeout(m.show, 0);
// Hello!

classを使う側で対処

index.js
'use strict';

class Message {
  constructor(message) {
    this.message = message;
  }
  show() {
    try {
      console.log(this.message);
    } catch (e) {
      console.error(e);
    }
  }
}

var m = new Message('Hello!');

m.show();
// Hello!

var show = m.show.bind(m);
show();
// Hello!

setTimeout(m.show.bind(m), 0);
// Hello!

setTimeout(() => {
  m.show();
}, 0);
// Hello!

ソースコード: https://github.com/boushi-bird/sample-es2015-class

4
4
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
4
4