David Colling
David Colling

Reputation: 11

Why is ReferenceError being thrown?

I simply cannot see how eventCards is not initialized. Can someone show me?

ReferenceError: Cannot access uninitialized variable. Line 7:26

var eventCards = [
    'Do1',
    'Do2',
    'Do3',
    'Do4'
];
var eventDeck = new Deck(eventCards);

class Deck {
    constructor(cards) {
        this.cards = cards;
        this.deck = shuffle(cards);
    }

    shuffle(array) {
        ...
    }

    drawTopCard() {
        ...
    }
}

Upvotes: 1

Views: 1106

Answers (1)

StaticBeagle
StaticBeagle

Reputation: 5175

For class variables, you need to define the class before it's used. Just hoist the class definition to the top and it should be ok:

class Deck {
    constructor(cards) {
        this.cards = cards;
        this.deck = shuffle(cards);
    }

    shuffle(array) {
        ...
    }

    drawTopCard() {
        ...
    }
}

var eventCards = [
    'Do1',
    'Do2',
    'Do3',
    'Do4'
];
var eventDeck = new Deck(eventCards);

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Upvotes: 3

Related Questions