how to define a static property in the ES6 classes

Amir Azarbashi picture Amir Azarbashi · Dec 28, 2017 · Viewed 37.5k times · Source

I want to have a static property in an ES6 class. This property value is initially an empty array.

    class Game{

        constructor(){
           // this.cards = [];
        }

        static cards = [];
    }
    
    Game.cards.push(1);

    console.log(Game.cards);

How can I do it?

Answer

zagoa picture zagoa · Dec 28, 2017
class Game{
   constructor(){}
}
Game.cards = [];

Game.cards.push(1);
console.log(Game.cards);

You can define a static variable like that.