简体   繁体   English

JavaScript Class Inheritance - 填充方法不起作用

[英]JavaScript Class Inheritance - fill method not working

Problem问题

I am attempting to create two classes, where one is a subclass of another, and populate the private variables in the constructor via fill method.我正在尝试创建两个类,其中一个是另一个的子类,并通过填充方法填充构造函数中的私有变量。 However, when using the fill method to populate the parent class private variables, these variables disappear when initializing the sub class.但是,当使用填充方法填充父 class 私有变量时,这些变量会在初始化子 class 时消失。

class RequestFile {
    constructor (args = {}) {
        // this.to = args.to ? args.to : []
        // this.files = args.files ? args.files : []
        // this.body = args.body ? args.body : ''
    }
    fill ({to=[],files=[], subject='', body='', }) {
      this.to = to
      this.files = files
      this.subject = subject
      this.body = body
    }
}

class Mail extends RequestFile {
    constructor (args = {}) {
      super(args)
      this.fill(args)
    }
    fill ({cc='', draft=false, preview=true}) {
      this.cc = cc
      this.draft = draft
      this.preview = preview
    }
    to() {
      console.log(this.to)
    }
}
let mail = new Mail({
  to: [ 'name@gmail.com' ],
  files: [ 1111 ],
  subject: 'Sent from Node',
  body: 'test body  -- sent from node',
  cc: [ 'anotherone@gmail.com', 'another@gmail.com' ],
  draft: true,
  preview: true
})

console.log(mail)

The above outputs the following:以上输出如下:

Mail {
cc:(2) [...],
draft: true ,
preview: true
}

Uncommenting the code in RequestFile Class yields a different result:取消注释 RequestFile Class 中的代码会产生不同的结果:

class RequestFile {
    constructor (args = {}) {
        this.to = args.to ? args.to : []
        this.files = args.files ? args.files : []
        this.body = args.body ? args.body : ''
    }
    fill ({to=[],files=[], subject='', body='', }) {
      this.to = to
      this.files = files
      this.subject = subject
      this.body = body
    }
}

Mail {
to:(1) [...],
files:(1) [...],
body: "test body -- sent from node" ,
cc:(2) [...],
draft: true ,
preview: true
}

Ideally, I would like to rely on the fill method to populate the class variables, I'm just confused as to why it works in the subclass (Mail) but doesn't work in the parent class (RequestFile)理想情况下,我想依靠填充方法来填充 class 变量,我只是对为什么它在子类(邮件)中有效但在父类 class(请求文件)中无效感到困惑

From the fill() method in the child class, call the inherited fill() method of the base class.从子 class 中的fill()方法,调用基 class 的继承fill()方法。 Use the special arguments variable to pass on the original parameter before object deconstruction was applied.在应用 object 解构之前,使用特殊的arguments变量传递原始参数。

You can then even move the call to fill() to the base class constructor and don't have to call the fill method from the child class constructor.然后,您甚至可以将对fill()的调用移至基本 class 构造函数,而不必从子 class 构造函数调用填充方法。

class RequestFile {
    constructor (args = {}) {
        this.fill(args);
    }

    fill ({to=[],files=[], subject='', body='', }) {
      this.to = to
      this.files = files
      this.subject = subject
      this.body = body
    }
}

class Mail extends RequestFile {
    constructor (args = {}) {
      super(args)
    }

    fill ({cc='', draft=false, preview=true}) {
      // pass on original parameter to base class fill method
      super.fill(arguments[0])
      this.cc = cc
      this.draft = draft
      this.preview = preview
    }
    to() {
      console.log(this.to)
    }
}

An alternative solution is to use different names for the fill methods in the base and child classes:另一种解决方案是为基类和子类中的填充方法使用不同的名称:

class RequestFile {
    constructor (args = {}) {
        this.fillRequestFile(args);
    }

    fillRequestFile ({to=[],files=[], subject='', body='', }) {
      this.to = to
      this.files = files
      this.subject = subject
      this.body = body
    }
}

class Mail extends RequestFile {
    constructor (args = {}) {
      super(args)
      this.fillMail(args);
    }

    fillMail ({cc='', draft=false, preview=true}) {
      this.cc = cc
      this.draft = draft
      this.preview = preview
    }
    to() {
      console.log(this.to)
    }
}
class Mail extends RequestFile {
    constructor (args = {}) {
      super()
      super.fill(args)
      this.fill(args)
    }
    fill ({cc='', draft=false, preview=true}) {
      this.cc = cc
      this.draft = draft
      this.preview = preview
    }
    to() {
      console.log(this.to)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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