Transcript of the tutorial.
TOP
So then we've seen now how to create a subclass which inherits from or extends another class, in our
case, the user class.
So it inherits all of these properties and all of these methods.
But also we've defined an extra method delete user, which only admins will have.
So that's all good and that works.
But what if now we want to add additional properties to only admins, so we don't want those properties
to be defined up here in the user because we don't want users to have them.
We only want admins to have these additional properties, for example a custom title property.
Well, to do that we need to define our own constructor inside the admin class because remember that
is where properties are defined inside a class.
So let's create this constructor first of all.
And inside here we could define those extra properties only for an admin.
Now say for example, I want to pass in another argument into an admin which is going to be a title,
a custom title.
So I'll do that as a third argument and I'll pass in black hyphen belt hyphen ninja.
Now because we have a constructor in this admin class now when we create a new admin, it's going to
run this constructor and it's not going to run this constructor anymore because this constructor only
ever runs when inside the child class a constructor doesn't exist.
So what we need to do now is first of all take in these three parameters.
So we have the username, we have also the email and then the custom title at the end, the third argument.
So in here, how do we attach the username and the email?
Well, we could ask to run this constructor and the way we do that is by calling super.
So when we call super inside a constructor, it looks for the parent class and it looks inside that
for a constructor.
And where that exists, it runs that constructor to set up properties for this object as well.
So we're going to run super, but we need to pass in these two arguments a username and an email and
we have those here, a username and an email.
So now if we were to run this, it would attach these two properties via this constructor and we'd also
get this score as well.
If we didn't have this here, then we wouldn't get those properties on our new object.
I'll demo that first in a second.
But anyway, now we want to also attach the title.
So I'm going to say this dot title is equal to title, and what I'm going to do now is just log user
three to the console.
So save that and preview over here and you can see we get an error.
It says must call super constructor in derived class before accessing this or returning from derived
constructor.
So before we even try to add any additional properties we have to call super.
It's saying we have to do that.
So we're doing that first of all.
Then it's attaching these properties to our new admin object and then we're attaching the title to the
admin object.
So if we save now, we should be able to see an admin has a username, it has an email, a score and
a title now and only admins will have this title.
Regular users won't.
So that my friends, is JavaScript and subclasses.
It's something that we'll be implementing in any future projects that we create.
But now we know how to use classes and we understand what's happening on the surface.
I'd like to start to peel back the layers and look at what's going on under the hood, and we'll start
that in the next video.