LoginSignup
0
0

More than 1 year has passed since last update.

アロー関数でコンストラクタを書いたら「TypeError: HogeClass is not a constructor」と言われてしまった件

Posted at

現象

  • これは、問題なくインスタンスすることができる。
const TestClass = function (className) {
  this.className = className;
};
new TestClass('hogehoge');
  • コンストラクタをアロー関数に変更するとエラーとなる。
const TestClass = className => {
  this.className = className;
};
new TestClass('hogehoge'); // TypeError: TestClass is not a constructor

理由

  • コンストラクタは、アロー関数で書くことができない。
    • そもそも、アロー関数のthisは宣言された時点でのthisになる。
0
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
0
0