简体   繁体   English

Class inheritance - 添加参数而不重写父构造函数 arguments

[英]Class inheritance - add argument without rewriting parent constructor arguments

I would like to extend the following class:我想扩展以下 class:

class Person {
    constructor(name, age) {
        this._name = name;
        this._age = age;
    }
  
    get name() {
        return this._name;
    }
  
    get age() {
        return this._age;
    }
}

...by adding sport and its getter: ...通过添加sport及其吸气剂:

class Athlete extends Person {
    constructor(name, age, sport) {
        super();
        this._name = name;
        this._age = age;
        this._sport = sport;
    }
  
    get sport() {
        return this._sport;
    }
}

While the above works, I would like to avoid repeating the arguments of the base-parent constructor.虽然上述方法有效,但我想避免重复基父构造函数的 arguments The following approach won't work:以下方法不起作用:

class Athlete extends Person {
    constructor(sport) {
        super();
        this._sport = sport;
    }
  
    get sport() {
        return this._sport;
    }
}

let athlete = new Athlete('Peter', 29, 'cricket');
console.log(athlete.name, athlete.age, athlete.sport); // name and age are not inherited, is returned 'undefined'

So, how can I add fields to the subClass without rewriting the ones of the base Class?那么,如何在不重写基本 Class 的情况下向子类添加字段?

You need to pass the arguments, but you can avoid to assign all of them by passing them to super() :您需要传递 arguments,但您可以通过将它们传递给super()来避免分配所有它们:

class Athlete extends Person {
    constructor(name, age, sport) {
        super(name, age); // pass arguments to super()
        this._sport = sport;
    }
  
    get sport() {
        return this._sport;
    }
}

let athlete = new Athlete('Peter', 29, 'cricket');
console.log(athlete.name, athlete.age, athlete.sport);

What you want can be achieved (using destructured parameters ( ...args ) syntax to denote an arbitrary number of arguments), however, it is not advised (at least the way I did it), and it comes at certain costs, some of which are readability, ease of debugging, lesser error-prone code, and more (since this makes the code a little more obscure, making the order of the arguments not that clear and making it possible to pass the wrong number of arguments):可以实现您想要的(使用解构参数...args )语法来表示任意数量的参数),但是,不建议这样做(至少我这样做的方式),并且它需要一定的成本,一些其中包括可读性、易于调试、不易出错的代码等等(因为这会使代码更加晦涩难懂,从而使 arguments 的顺序不那么清晰,并且可以传递错误数量的参数):

 class Person { constructor(name, age) { this._name = name; this._age = age; } get name() { return this._name; } get age() { return this._age; } } class Athlete extends Person { constructor(...args) { let sport = args.splice(-1)[0]; super(...args); this._sport = sport; } get sport() { return this._sport; } } let athlete = new Athlete('Peter', 29, 'cricket'); console.log(athlete.name, athlete.age, athlete.sport);

It can similarly be achieved using the arguments object:使用arguments object 可以类似地实现它:

 class Person { constructor(name, age) { this._name = name; this._age = age; } get name() { return this._name; } get age() { return this._age; } } class Athlete extends Person { constructor() { let args = Array.from(arguments); let sport = args.splice(-1)[0]; super(...args); this._sport = sport; } get sport() { return this._sport; } } let athlete = new Athlete('Peter', 29, 'cricket'); console.log(athlete.name, athlete.age, athlete.sport);

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

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