简体   繁体   中英

Object.keys returns an empty array for an object containing properties

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:

在此处输入图片说明

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

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

在此处输入图片说明

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.

在此处输入图片说明

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

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

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM