【JavaScript 】クラスの継承

【JavaScript 】クラスの継承:


クラスの継承

'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

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)