【JavaScript 】クラス

【JavaScript 】クラス:


クラス

'use strict'; 
 
class Player { // クラス名は慣習的に大文字から始める 
 
    constructor(name, score) { // コンストラクター 
        // プロパティ 
        this.name = name; 
        this.score = score; 
    } 
 
    // メソッド 
    showScore() { 
        console.log(` score = ${this.score}`); 
    } 
 
    // 静的メソッド 
    static showVersion() { 
        const version = '1.0'; 
        console.log(`Player class ver. ${version}`); 
    } 
} 
 
// インスタンス 
const player1 = new Player("player1", 10); 
 
// プロパティ 
console.log(player1.name); // player1 
 
// メソッド 
player1.showScore(); // score = 10 
 
// 静的メソッド 
Player.showVersion(); // Player class ver. 1.0 
※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件)