super()
Hey, am i using
super()
correctly in this case? I had a bit a trouble understanding the functionality.
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
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 statici know, i was checking if i was using it correctly?
which is not what you want
you are, yes
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
both
oh so i need to do this?
and then the same for
type
in Spider
?no
oh?
inside the class
oh yeah just changed that haha
realised just then
okay so if it's inside the class, then like that?
yes, like that
okay cool, thanks for the clarification on everything
this.species
<-- this also shouldn't exist, as far as i know
no, it workssorry, what do you mean?
🤔
oh okay
why did you think it wouldn't?
because, usually, you use
super
to access thoselike
super.species
?yeah
yeah i've seen that as well
but if it works, then it works
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?i don't know why but it doesn't work 🤔
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.if you try to access the value before
this.species
, it wouldn't existdoes it say it's
undefined
for you when you do that?yes
with
super.species
yeah that's the same here
did they perhaps change the way it works?
no, but i might have misunderstood it
oh okay
so if i tried to access it before i defined it in the constructor it wouldn't exist?
exactly
ah okay i see
thank for your help
you're welcome