super()

Hey, am i using super() correctly in this case? I had a bit a trouble understanding the functionality.
class Animal {
constructor(species) {
this.species = species
}
get Species() {
return this.species
}
}

class Spider extends Animal {
constructor(species, type) {
super(species)
this.type = type
}
get SpeciesReason() {
return `I'm an ${this.species} because I have 8 legs.`
}
}

let spider = new Spider("Arachnid", "House Spider");
class Animal {
constructor(species) {
this.species = species
}
get Species() {
return this.species
}
}

class Spider extends Animal {
constructor(species, type) {
super(species)
this.type = type
}
get SpeciesReason() {
return `I'm an ${this.species} because I have 8 legs.`
}
}

let spider = new Spider("Arachnid", "House Spider");
I've played around with it and found the following which makes me think that i'm doing everything right as it works, but i might be wrong. 1) If i don't include super() and species in the constructor args for the Spider class, since it has it's own constructor it throws an error saying must call super constructor before using 'this' in derived class constructor as i'm trying to use this.species. 2) If i don't include species in the constructor args but have a super(species) it throws an error saying species isn't defined. 3) If i include species in the constructor args but don't include super(species) it throws an error saying must call super constructor before using 'this' in derived class constructor. I'd appreciate any insight. Thanks in advance.
33 Replies
ἔρως
ἔρως3d ago
you didn't define the public property species in the class Animal you also didn't define type in Spider calling super is mandatory - no way around it unless EVERYTHING is static
snxxwyy
snxxwyy3d ago
i know, i was checking if i was using it correctly?
ἔρως
ἔρως3d ago
which is not what you want you are, yes
snxxwyy
snxxwyy3d ago
also i'm confused about the first two things you said, are you saying those in reference to the errors, or are they just general things i'm not doing? oh cool, glad i got that down
ἔρως
ἔρως3d ago
both
snxxwyy
snxxwyy3d ago
oh so i need to do this?
class Animal {
species;

constructor(species) {
this.species = species
}
get Species() {
return this.species
}
}
class Animal {
species;

constructor(species) {
this.species = species
}
get Species() {
return this.species
}
}
and then the same for type in Spider?
ἔρως
ἔρως3d ago
no
snxxwyy
snxxwyy3d ago
oh?
ἔρως
ἔρως3d ago
inside the class
snxxwyy
snxxwyy3d ago
oh yeah just changed that haha realised just then okay so if it's inside the class, then like that?
ἔρως
ἔρως3d ago
yes, like that
snxxwyy
snxxwyy3d ago
okay cool, thanks for the clarification on everything
ἔρως
ἔρως3d ago
this.species <-- this also shouldn't exist, as far as i know no, it works
snxxwyy
snxxwyy3d ago
sorry, what do you mean?
ἔρως
ἔρως3d ago
🤔
snxxwyy
snxxwyy3d ago
oh okay why did you think it wouldn't?
ἔρως
ἔρως3d ago
because, usually, you use super to access those
snxxwyy
snxxwyy3d ago
like super.species?
ἔρως
ἔρως3d ago
yeah
snxxwyy
snxxwyy3d ago
yeah i've seen that as well
ἔρως
ἔρως3d ago
but if it works, then it works
snxxwyy
snxxwyy3d ago
do you think it's recommended to use super.species to be safe? or as a more general thing, super.xyz if you're using a value from a parent class?
ἔρως
ἔρως3d ago
i don't know why but it doesn't work 🤔
snxxwyy
snxxwyy3d ago
also a quick question regarding this. What would happen if i didn't include species; at the top? The only thing i could think of is that it wouldn't allow you to change it to a private property with # if you wanted.
ἔρως
ἔρως3d ago
if you try to access the value before this.species, it wouldn't exist
snxxwyy
snxxwyy3d ago
does it say it's undefined for you when you do that?
ἔρως
ἔρως3d ago
yes with super.species
snxxwyy
snxxwyy3d ago
yeah that's the same here did they perhaps change the way it works?
ἔρως
ἔρως3d ago
no, but i might have misunderstood it
snxxwyy
snxxwyy3d ago
oh okay so if i tried to access it before i defined it in the constructor it wouldn't exist?
ἔρως
ἔρως3d ago
exactly
snxxwyy
snxxwyy3d ago
ah okay i see thank for your help
ἔρως
ἔρως3d ago
you're welcome
Want results from more Discord servers?
Add your server