问题
In JS, I have a class called player
which is:
class player {
constructor(name) {
this.name = name;
}
}
and I have two instances of it, called PL1
and PL2
:
const PL1 = new player ('pl1name');
const PL2 = new player ('pl2name');
I also have an array called PLAYERS
:
let PLAYRES = [];
now, the question is how can I create an array with all of the instances of the class player
?
I know I can manually do this with PLAYERS.push(PL
n)
; but I'm looking for a way to do this somehow automatically. Is there a built-in function? Should I use a loop?
回答1:
You could create a class that is a container class for the players. This will allow the container to create players and manage them. The the Players
class can expose an interface making it easy to interact with the players individually or as a whole. Something like this might be a good start and could be filled out with more functionality or different orginziation:
// An individual player. Holds properties and behavior for one player
class Player {
constructor(name) {
this.name = name;
}
play() {
console.log(this.name, "plays")
}
}
// Class that holds a collection of players and properties and functions for the group
class Players {
constructor(){
this.players = []
}
// create a new player and save it in the collection
newPlayer(name){
let p = new Player(name)
this.players.push(p)
return p
}
get allPlayers(){
return this.players
}
// this could include summary stats like average score, etc. For simplicy, just the count for now
get numberOfPlayers(){
return this.players.length
}
}
let league = new Players()
league.newPlayer("Mark")
league.newPlayer("Roger")
// list all the players
console.log(league.numberOfPlayers + " Players)
console.log(league.allPlayers)
// make them do something
league.allPlayers.forEach(player => player.play())
回答2:
To initialize an Array with a static amount of Player
objects, you could call new Player()
in the array:
const players = [new Player('name1'), new Player('name2'), new Player('name3')];
You could also dynamically create your player list using a loop:
const playerNames = ['name1', 'name2', 'name3'];
let players = [];
playerNames.forEach((playerName) => players.push(new Player(playerName)));
回答3:
I found an answer for doing this when creating a new instance by changing my class to:
class player {
constructor(name){
this.name = name;
PLAYERS.push(this);
}
}
回答4:
I can't comment your answer as of now so here's an addition:
class player {
constructor(name){
this.name = name;
PLAYERS.push(this);
}
}
PLEASE: be aware there's a whole lot of awful practices in this few lines, you shouldn't tie a constructor to a variable that may or may not be initialized somewhere else, meddling with the constructor with whatever external is plain awful. Also classes are usually title-cased.
Plus as a side note, while it's real that you can change consts properties while maintaining the reference, so you can push objects in an array that's declared as const, it really doesn't add up as self-documenting code, so, if you are going to modify this array, just declare it with "let" from the start.
来源:https://stackoverflow.com/questions/52377344/javascript-array-of-instances-of-a-class