简体   繁体   English

如何将类构造函数的先前实例的参数传播到另一个实例

[英]How to spread the parameters of a previous instance of a class constructor to another instance

I would like to ask what I am doing wrong here 我想问一下我在做什么错

My goal 我的目标

I want to create instances from a class constructor. 我想从类构造函数创建实例。 The first is gonna be a more generic class called Person and then another that will inherit properties from that class. 首先是一个更通用的类Person,然后是另一个将从该类继承属性的类。

My question is When all classes are set and the first instance that points to the Person constructor is declared, how could the pass the key: values of the previous instance to the next instance since I don't want to repeat my self over the same arguments. 我的问题是,当所有类都已设置并且声明了指向Person构造函数的第一个实例时,如何将key: values前一个实例的key: values传递给下一个实例,因为我不想在同一实例上重复我的自我参数。

I am currently spreading the previous parameters of the instance but obviously, I am doing something wrong. 我目前正在传播实例的先前参数,但显然,我做错了。

class Person {
    constructor (name,yearOfBirth,job) {
        this.name = name;
        this.yearOfBirth = yearOfBirth;
        this.job = job;
    }
    getAge() {
        return new Date().getFullYear() - this.yearOfBirth
    }
    greet(){
        return `${this.name} is a ${this.getAge()} years old ${this.job}`
    }
}

class footballPlayer extends Person {
    constructor(name,yearOfBirth, job, team, cups) {
        super(name, yearOfBirth, job)
        this.team = team;
        this.cups = cups;
    }
    cupsWon() {
        console.log(`${this.name} who was bord on ${this.year} and works as a ${this.job} won ${this.cups} with ${this.team}`);
    }
}

const vagg = new Person('vaggelis', 1990, 'Developer');
const vaggA= new footballPlayer( {...vagg} , 'real madrid', 4)

console.log(vagg.greet());
console.log(vaggA.cupsWon());

Thank you! 谢谢!

If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person to the footballPlayer (note: class names should by convention be uppercase). 如果我正确理解了您想做什么,则只需将描述Person的参数的值传递给footballPlayer (注意:类名按照惯例应为大写)。

var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);

Edit : In case you fear a different order with Object.values (which is a real threat ), you can create a getter function in the Person class that will return the exact list of parameters in the order they are supposed to be given to the constructor: 编辑 :如果您担心与Object.values不同的顺序(这是一个真正的威胁 ),则可以在Person类中创建一个getter函数,该函数将按应将给定参数的顺序返回参数的确切列表。构造函数:

class Person {
  // ...
  describe() {
    return [this.name, this.yearOfBirth, this.job];
  }
}

const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);

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

相关问题 传播一个 class 实例 - Spread a class instance 如何将 object(类实例属性)传播到父级的超级构造函数? - How do I spread an object (class instance properties) to the parent's super constructor? 使用构造函数参数实例化打字稿类实例 - instantiate typescript class instance with constructor parameters 打字稿:在另一个Class构造函数中创建Class实例的数组 - Typescript: Creating Array of Class instance inside another Class constructor 如何使 JavaScript 类实例成为另一个类的实例? - How can I make a JavaScript class instance be instance of another class? 从构造函数初始化单个实例,并将其用作另一个类中的静态方法 - Initiate single instance from a constructor and use it as static method in another class Undefined不是尝试在另一个类中创建一个类的新实例的构造函数 - Undefined is not a constructor trying to create new instance of one class inside another 在nodejs中,如何使具有构造函数具有不同参数的模块的单个实例? - In nodejs how to have single instance of module having constructor with different parameters? 如何检查实例的构造函数 - how to check constructor of instance 注释由构造函数参数设置的实例属性? - Annotating instance properties which are set by constructor parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM