0

My project contains an ES6 class whose constructor is defined as follows:

  constructor() {
    this.campaignName = 'United Way';
    this.campaign = {};
    this.questions = {};
    this.benefits = {};
    this.assistors = {};
    this.locations = {};
    this.buildDataObjects = this.buildDataObjects.bind(this);
    this.retrieve();
  }

Later in the code, the properties with default values of empty objects are given new values using Object.assign; however, I do not believe that code has any relevance for solving the problem at hand, so it is not included.

Elsewhere in the project's codebase, an instance of the class is being output using console.info:

enter image description here

As evident from the screenshot, there is seemingly nothing unusual about the object.

Next, the object's "questions" property is output:

enter image description here

The first line in the screenshot is an empty object, even though the output below includes the object's properties. However, those properties are inaccessible, as evident by the fact that calling Object.keys with the given object produces an empty array.

enter image description here

I am at a total loss as to the cause of this behavior and would appreciate some insight. Thanks in advance!

Boris Layvant
  • 151
  • 13
  • The fact that the object shows as `{}` and afterwards has elements tells you that the questions only exist **after** you log them in the console. Hence the _how_ and _when_ the "questions" are generated/assigned does matter. – Andreas Nov 22 '18 at 17:36
  • Interesting...the second line is part of the same console.info output so how can they only exist after they're logged? – Boris Layvant Nov 22 '18 at 17:40
  • 1
    [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Andreas Nov 22 '18 at 17:44
  • @Andres you are correct. It turns out that the assignment of a new value for questions was taking place after console.info was called. – Boris Layvant Nov 22 '18 at 18:05

1 Answers1

0

As @Andreas pointed out, the console output was being performed before the assignment of a new value to "questions" was completed.

enter image description here

As a side-note, my confusion was exacerbated by the presence of properties below the empty object in the console output, which are apparently somehow appended after the fact.

Boris Layvant
  • 151
  • 13