流javascript类属性被未定义覆盖

时间:2018-11-09 10:17:46

标签: javascript node.js flowtype

我有两个课

export class Model {
_id: ID

  constructor(arg : ID | string | Object) {
   if (typeof arg === 'object') this.loadObject
 }


  loadObject(obj: Object) {
    Object.assign(this, obj)
    // using a for loop to assign keys does not work with flow
  }
}

export class User extends Model {
_id: ID
username: string
//...other properties
}

当我调用new Model(userObject)时,所有属性都已定义。但是,当我调用new User(userObject)时,仅定义了一些属性,而有些则未定义。为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您忘记致电this.loadObject,而需要在super的{​​{1}}中致电User

尝试一下:

constructor

enter image description here