JS/TSのclassでclass名を取得する
約 2 分
目次
概要
JS/TSのconstructorを利用して自分自身のクラス名を取得する際のメモ。
コード
class BaseApp {
constructor() {
console.log('1:' + this.constructor.name);
console.log('2:' + this.getClassName());
}
getClassName() {
return this.constructor.name;
}
}
class App extends BaseApp {
constructor() {
super();
console.log('3:' + this.constructor.name);
console.log('4:' + super.constructor.name);
}
}
class ExApp extends App {
constructor() {
super();
console.log('5:' + this.constructor.name);
console.log('6:' + super.constructor.name);
}
}
const app = new ExApp();
console.log('7:' + app.constructor.name);
このコードの結果は次のようになります。
1:ExApp
2:ExApp
3:ExApp
4:BaseApp
5:ExApp
6:App
7:ExApp
おすすめの記事
最新の記事
よく読まれている記事
タグから探す
- javascript130
- typescript67
- node.js54
- linux54
- 画像処理48
- amazon-aws48
- アルゴリズム37
- canvas35
- html529
- 画像処理100本ノック27
- php24
- centos24
- python22
- 競技プログラミング21
- mac21
- mysql20
- opencv17
- 雑談16
- 機械学習16
- docker16
- wordpress15
- atcoder14
- apache12
- データベース12
- amazon-s312
- red-hat12
- prisma12
- ubuntu11
- github10
- git10
- react10
- mariadb10
- vue.js9
- next.js9
- aws-cdk9
- css38
- 可視化8
- 小ネタ8
- nestjs8
- amazon-lightsail7
- ブログ6
- cms6
- oracle6
- perl6
- gitlab6
- iam5
- amazon-ec25
- 資格試験5
- aws-amplify5
- curl4