1

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:

  • super(client) and super(client, {somestuff}) - Doesn't work obviously, because I am chaging the DNA of the Lion that has been declared beforehand.
  • Having an empty variable let dnaand then doing dna = DNA and then exporting it - 'obviously' did not work.
9
  • 1
    The question doesn't really make sense; when you call super() you can either pass DNA or not pass it. What would make sense (and be consistent with actual reality) would be for there to be a method on Lion to make a new BabyLion. You have to have an instance of Lion, in other words. Commented Aug 21, 2018 at 14:39
  • But I want to pass the DNA that the user specified when declaring the Lion class. Commented Aug 21, 2018 at 14:41
  • You're confusing the declaration of the class and the instantiation of a Lion instance. You could also have one of the arguments to the BabyLion constructor be the parent Lion instance. Commented Aug 21, 2018 at 14:42
  • 1
    @dorintufar If BabyLion doesn't declare a DNA parameter, then no, it's not accessible. Whence would it come? Commented Aug 21, 2018 at 14:47
  • 1
    in fact, you can access DNA (with your implementation) this way: set DNA as class property in Lion class then access property in descendant class (BabyLion) after super call Commented Aug 21, 2018 at 14:58

4 Answers 4

2

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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0

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();

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.