LoginSignup
1
2

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}`);
    }

    // 静的メソッド
    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

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