【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

コメント

このブログの人気の投稿

投稿時間:2020-07-01 05:34:30 RSSフィード2020-07-01 05:00 分まとめ(37件)

投稿時間:2020-07-01 20:43:40 RSSフィード2020-07-01 20:00 分まとめ(81件)

投稿時間:2020-07-01 08:33:22 RSSフィード2020-07-01 08:00 分まとめ(49件)