Mxm
Mxm

Reputation: 97

javascript how to get parent class constructor parameters

I am pretty stuck right now on this, how can I get a parent class constructor's parameter? I want to get the Lion's DNA, then pass it to the baby lion and use it. Note that this isn't really for my personal use but rather it's a package / module, so I can't enter what I've entered in my code.

Example code:

class Lion {
    constructor(client, DNA = {}) {
        this.sharp = howsharp;   
        this.client = client;
    }
}

class BabyLion extends Lion {
    constructor(client) {
        super(client, How can I get DNA??);
    }
}

Things I've tried:

Upvotes: 0

Views: 2438

Answers (4)

Scott Sauyet
Scott Sauyet

Reputation: 50787

It's pretty unclear what you want to do. But if you want to mimic parents -> child inheritance, then something like this could work:

const mix = (dna1, dna2) => Object.keys(dna1).reduce(
    (strand, key) => Object.assign(strand, {[key]: Math.random() < .5 
      ? dna1[key] 
      : dna2[key]
    }), {}
)


class Lion {
    constructor(name, DNA = {}) {
        this.name = name
        this.DNA = DNA;   
    }
    
    mate(partner, name) {
        return new Lion(name, mix(this.DNA, partner.DNA))
    }
}

const father = new Lion('Mufasa', {foo: 1, bar: 2, baz: 3, qux: 4})
const mother = new Lion('Surabi', {foo: 5, bar: 6, baz: 7, qux: 8})

const child = mother.mate(father, 'Simba')

console.log(child)

This does not use inheritance as used in software. (A baby lion is still just another lion.) But the baby does (randomly) inherit its genes from each parent.

And I'm assuming that this has little to do with what you actually want. But beware of thinking too literally about inheritance if this is just a dummy example. There is a real difference between real-world inheritance and the programming concept.

Upvotes: 0

Vivek
Vivek

Reputation: 86

If this helps.

class Lion {
    constructor(client, DNA = {}) {
        this.sharp = 'howsharp';   
        this.client = client;
        this.dna = DNA;
    }
}

class BabyLion extends Lion {
    constructor(client) {
        super(client);
    }
    printDna() {
        console.log(this.dna);
    }
}

var baby = new BabyLion('hoper');
baby.printDna();

Upvotes: 0

Javier
Javier

Reputation: 31

class Lion {
 constructor(sharp, client) {
  this.sharp = sharp
  this.client = client;
 }

 getSharp(){
  return this.sharp;
 }
}

class BabyLion extends Lion {
 constructor(sharp, client) { // Here you can use more BabyLion parameters 
  super(sharp, client);
 }
}

a = new BabyLion(1, 2)
a.getSharp(); // returns ==> 1. This way it gets other attribures from Lion class

Upvotes: 0

deceze
deceze

Reputation: 522042

You don't "get the parent's parameter", you need to define all the necessary parameters in your overridden constructor and then pass those to super():

class BabyLion extends Lion {
  constructor(client, DNA) {
    super(client, DNA);
  }
}

You may add more parameters to BabyLion's constructor, or fewer, but your child's constructor needs to somehow get and pass all required parameters to the parent's constructor. E.g.:

class BabyLion extends Lion {
  constructor(client) {
    super(client, 'foo');
  }
}

class BabyLion extends Lion {
  constructor(client, DNA, bar, baz) {
    super(client, DNA);
    this.bar = bar;
    this.baz = baz;
  }
}

Upvotes: 1

Related Questions