クラスの継承
'use strict';
// 親クラス
class Player {
constructor(name, score) {
this.name = name;
this.score = score;
}
showScore() {
console.log(`score = ${this.score}`);
}
}
// 子クラス
class GamePlayer extends Player {
constructor(name, score, high_score) {
super(name, score); // 親のコンストラクターを呼び出す
this.high_score = high_score;
}
showHighScore() {
console.log(`high_score = ${this.high_score}`);
}
}
// インスタンス
const game_player1 = new GamePlayer("GamePlayer1", 10, 100);
// プロパティ
console.log(game_player1.high_score); // 100
// プロパティ(親)
console.log(game_player1.name); // GamePlayer1
// メソッド
game_player1.showHighScore(); // high_score = 100
// メソッド(親)
game_player1.showScore(); // score = 10
※Google Chrome で確認
※ES8