代码之家  ›  专栏  ›  技术社区  ›  Boris Layvant

Object.keys返回包含属性的对象的空数组

  •  0
  • Boris Layvant  · 技术社区  · 6 年前

    我的项目包含一个ES6类,其构造函数定义如下:

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

    在后面的代码中,使用Object.assign为具有空对象默认值的属性赋予新值;但是,我不认为代码与解决当前问题有任何关联,因此不包括它。

    在项目代码库的其他地方,使用console.info输出类的实例:

    enter image description here

    接下来,输出对象的“questions”属性:

    enter image description here

    截图中的第一行是一个空对象,即使下面的输出包含该对象的属性。但是,这些属性是不可访问的,正如调用 Object.keys

    enter image description here

    我完全不知道这种行为的原因,希望能有所了解。提前谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Boris Layvant    6 年前

    正如@Andreas指出的,控制台输出是在完成“问题”的新值赋值之前执行的。

    enter image description here

    顺便说一下,控制台输出中的空对象下面有一些属性,这些属性显然是在事实之后附加的,这加剧了我的困惑。