In Javascript,I want to use a spread operator in a class, but I get TypeError: this.deck is not iterable.
Off screen, I have a Card class, and a Deck52 class.
The Deck52 class constructor has this.deck = []; this.createDeck(); the this.createDeck uses this.createDeck.push with some loops and variables to put 52 Card instances in the Deck52 this.deck. That seems to work all right, as far as I can tell.
The problem is –
In below, I have a BigDeck class.
class BigDeck() {
constructor(decksUsed = 6) {
this.decksUsed = decksUsed;
this.deck = [];
}
createDeck() {
for (let i = 0; i < this.decksUsed; i++) {
const newDeck = new Deck52();
this.deck = [...this.deck, ...newDeck.getDeck())] // this line is a problem.
}
}
}
When I try to do this, I get “TypeError: this.deck is not iterable”.
I got a similar message when trying to use newDeck.deck, so I put added getDeck() to Deck52. This seems to work all right; newDeck is an instance of Deck52 and newDeck.getDeck() functionally returns an array.
HOWEVER
In BigDeck, when I try to use this.deck, I get the message this.deck is not iterable. Which, I think “this” is a keyword or some such, anyways though I have this.deck = [] in constructor, typeof(this.deck) is undefined (tested with console.log).
WHAT I’M TRYING TO DO:
Eventually, pass arguments into BigDeck, and based on those arguments add different decks to BigDeck’s this.deck array. But apparently I can’t use the spread operator, and I don’t think I can use the spread operator in a setter method either, as fundamentally this.deck doesn’t have array properties.
I think I can maybe do something with [Symbol.iterator], but I don’t know the syntax, and can’t figure out how to add it from mdn documentation.
Thanks for any answers!