I am trying to invoke a class with the code below:
class matchDetails {
constructor(game, kit, player){
this.game = game; // This remains static;
this.kit = color; // This remains static;
this.player = new Array(player);
}
addMatchDetails(){
// return "Coventry";
}
}
ab = new matchDetails(23, 'red', 11);
On the constructor I want this.player to be an array where I can push additional values but I cannot fathom out how to code it.
The simplest approach is keeping extending your contructor and add lines. You can initialize the array and push the value. See the example below:
class matchDetails {
constructor(game, kit, player){
this.game = game;
this.kit = kit;
this.player = [];
this.player.push(player)
}
addMatchDetails(){
// return "Coventry";
}
}
ab = new matchDetails(23, 'red', 'smith');
//pushing additional surnames
ab.player.push('jones');
console.log(ab)
//matchDetails { game: 23, kit: 'red', player: [ 'smith', 'jones' ] }
In addition, there is no "color" variable, so I changed it to "kit".