Iām trying to dynamically create objects, using a loop counter. And want to use the counter itself in the naming the objects. The end result would be an array of players like this:
players = [
p1: {
//some data like age, score etc.
},
p2: {
//some data like age, score etc.
},
p3: {
//some data like age, score etc.
}
]
but my simple code below is not working.
var numPlayers = 3;
var p;
var player = {
rollDice : function(){
console.log('i am rolling the dice');
},
age: function(){
console.log("My name is " + this.age);
},
score:0
}
for(i=0;i<numPlayers;i++){
var p + i = Object.create(player);
console.log('player ' + p + i + " created!");
}
the problem is this line:
var p + i = Object.create(player);
i've tried various ways to make it work like
var 'p' + i = Object.create(player);
how can this be achieved?