0

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.

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.

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!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Evan
  • 583
  • 1
  • 5
  • 11
  • You cannot spread an object into an arguments *list*. You should make a constructor that takes an object instead. – Bergi Nov 22 '18 at 13:23

1 Answers1

0

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).

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:

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

const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
Zim
  • 1,457
  • 1
  • 10
  • 21
  • `Object.values` [does not guarantee order](https://stackoverflow.com/a/30919039/1048572), so this might easily break – Bergi Nov 22 '18 at 13:20
  • That's not true regarding `String` keys [in 2018](https://stackoverflow.com/a/31102605/2898738). This can be discussed but no need to downvote IMO. – Zim Nov 22 '18 at 13:39
  • Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification – Bergi Nov 22 '18 at 14:02
  • Your link tells the same about String keys: "Other string keys (if applicable), in property creation order." – Zim Nov 22 '18 at 14:04
  • Yes, but that property is guaranteed only for `Object.getOwnPropertyNames` etc, *not* for `Object.keys`, `Object.values` and `Object.entries`. – Bergi Nov 22 '18 at 14:16
  • Btw, even if they were in property creation order, this approach is far too fragile as it breaks when the constructor implementation changes. – Bergi Nov 22 '18 at 14:17
  • 1
    I've edited my answer to take into account your statements. Thanks for having me get aware of this. – Zim Nov 22 '18 at 14:26