1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【JavaScript 】クラスの継承

Posted at

クラスの継承

'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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?