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
ἔρως
ἔρως2mo 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
snxxwyyOP2mo ago
i know, i was checking if i was using it correctly?
ἔρως
ἔρως2mo ago
which is not what you want you are, yes
snxxwyy
snxxwyyOP2mo 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
ἔρως
ἔρως2mo ago
both
snxxwyy
snxxwyyOP2mo 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?
ἔρως
ἔρως2mo ago
no
snxxwyy
snxxwyyOP2mo ago
oh?
ἔρως
ἔρως2mo ago
inside the class
snxxwyy
snxxwyyOP2mo ago
oh yeah just changed that haha realised just then okay so if it's inside the class, then like that?
ἔρως
ἔρως2mo ago
yes, like that
snxxwyy
snxxwyyOP2mo ago
okay cool, thanks for the clarification on everything
ἔρως
ἔρως2mo ago
this.species <-- this also shouldn't exist, as far as i know no, it works
snxxwyy
snxxwyyOP2mo ago
sorry, what do you mean?
ἔρως
ἔρως2mo ago
🤔
snxxwyy
snxxwyyOP2mo ago
oh okay why did you think it wouldn't?
ἔρως
ἔρως2mo ago
because, usually, you use super to access those
snxxwyy
snxxwyyOP2mo ago
like super.species?
ἔρως
ἔρως2mo ago
yeah
snxxwyy
snxxwyyOP2mo ago
yeah i've seen that as well
ἔρως
ἔρως2mo ago
but if it works, then it works
snxxwyy
snxxwyyOP2mo 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?
ἔρως
ἔρως2mo ago
i don't know why but it doesn't work 🤔
snxxwyy
snxxwyyOP2mo 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.
ἔρως
ἔρως2mo ago
if you try to access the value before this.species, it wouldn't exist
snxxwyy
snxxwyyOP2mo ago
does it say it's undefined for you when you do that?
ἔρως
ἔρως2mo ago
yes with super.species
snxxwyy
snxxwyyOP2mo ago
yeah that's the same here did they perhaps change the way it works?
ἔρως
ἔρως2mo ago
no, but i might have misunderstood it
snxxwyy
snxxwyyOP2mo ago
oh okay so if i tried to access it before i defined it in the constructor it wouldn't exist?
ἔρως
ἔρως2mo ago
exactly
snxxwyy
snxxwyyOP2mo ago
ah okay i see thank for your help
ἔρως
ἔρως2mo ago
you're welcome
Want results from more Discord servers?
Add your server