0

I'm fairly new to Javascript, but trying to make an object. As far as I can tell document.gibado.Subway.Finals listed as undefined, but I don't understand why? Can someone explain this to me?

The error I'm getting says that

Cannot read property 'EMAIL' of undefined

This code doesn't work:

document.gibado.Subway = {
    Finals: {
        EMAIL: 'email',
        STORE_NUMBER: 'storeNumber',
        HIDE_OPTIONS: 'hideOptions'
    },

    hardQs: [{
        text: 'To what email address should we send your unique code?',
        answer: document.gibado.Subway.Finals.EMAIL
    }]
};

This code does work:

document.gibado.Subway = {
    Finals: {
        EMAIL: 'email',
        STORE_NUMBER: 'storeNumber',
        HIDE_OPTIONS: 'hideOptions'
    }
};

document.gibado.Subway.hardQs = [{
        text: 'To what email address should we send your unique code?',
        answer: document.gibado.Subway.Finals.EMAIL
}];
Gibado
  • 168
  • 1
  • 7
  • 1
    You can't refer to the object while you're defining it. It doesn't exist yet at that point. – JJJ Mar 30 '15 at 19:15
  • 1
    The variable `document.gibado.Subway` isn't assigned until the statement completes, but you're try to access it before it's done. – Barmar Mar 30 '15 at 19:16
  • I've also tried `answer: this.Finals.EMAIL` rather than `answer: document.gibado.Subway.Finals.EMAIL` and it still won't work. Why is it still considered undefined at that point? – Gibado Mar 30 '15 at 19:58
  • 1
    Again, you can't refer to something that doesn't exist, not even with `this`. – JJJ Mar 30 '15 at 19:58

0 Answers0