1

To stay organised whilst fetching a lot of DOM elements, I'm trying to store these in an object. (I may be adding DOM methods later)

Why can't I reference a property and get it's child after it's declared?

const DOM = {
    container: document.querySelector('.quiz'),
    question: this.container.children[0],
    labels: Array.from(document.querySelectorAll('.quiz__answer--label')),
    input: this.labels[0]
};

When 'container' and 'labels' are called by other properties they are undefined, why is this?

Is there any particular method good for organising a lot of DOM elements or is it best just to store them in variables?

Matt
  • 11
  • 1
  • The actual explanation seems to be missing in the linked dup. `this` is not defined at the time you're referring it, because the object you're constructing doesn't exist yet. The object will exist only after the assignment is completed. – Teemu May 05 '19 at 09:09

0 Answers0