Object Oriented JavaScript Tutorial #8 - Class Inheritance
Skills:
AI Pair Programming60%
Hey gang, in this object oriented JavaScript tutorial I'll teach you about Class Inheritance.
🐱💻 Course Links:
- VS Code editor - https://code.visualstudio.com/
- GitHub repository (course files) - https://github.com/iamshaunjp/object-oriented-js
- JavaScript for Beginners - https://www.youtube.com/watch?v=qoSksQ4s_hg&list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
👾 Video thumbnail artwork by PixelSwish @ https://www.youtube.com/channel/UCGKSD3mitWl5UpMxZzaIrRA
What You'll Learn
Teaches class inheritance in object-oriented JavaScript
Full Transcript
so then this is looking pretty good we have our user class and we can create these new instances of users down here and they have all these different methods and we could also do method chaining now so all of this is looking pretty good however and now want to introduce the concept of inheritance say for example on this website we have users right we could have as many users as you want one hundred two hundred etc and they all have these different methods and these different properties now also on the website I'd like to create some special kind of users which are admins right and these admins will have some different methods associated with them now at the end of the day these admins are still uses they're still going to have all the same properties like email name score and all the same functions like log in log out an update score however they're also can have extra methods like the ability to delete a user now I don't want to attach a delete user method to this thing right here because I don't want any user to be able to delete a user just admins so what we need to do really is create a new admin class with that method inside it but the problem is we also want all of this stuff inside the admin class as well because we want all of this same functionality so what we could do is create that class and we could copy and paste all of that into that I've been class and then also add on the extra delete user method or whatever other methods and admin might have as well however that's not the best way to do it instead what we could do is use plus inheritance so what we'd like to say is okay yeah we will create an extra admin class so let's create that first of all we'll say class admin like so and this admin class is actually gonna inherit from the user class because at the end of the day an admin is a user it's just a specific type of user with some extra bits and bobs inside it so we want to inherit all of this stuff from the user class but then just add some extras right now we can do that very easily using this Plus syntax and the way we do it is by saying okay yeah class admin then extends and this is gonna extend this class from another one which is use it okay so now whenever we create a new admin what it's going to do is still give it all of this stuff right here it's going to run this constructor to give it all of these different properties and associate all of these different methods with an admin when we create one so that's good we're creating this admin with all the same functionality and properties as a user now that was easy but now what we can do is pass in some extra functionality into this admin class which the user doesn't have so I wanna create a delete user function and by the way like I said we don't need the constructor in here if we don't have a constructor in the class that extends from another then it will just use this constructor right here okay which is what we're doing so delete user this is gonna be the method that I want to use for deleting a user now first of all what we need to do is create a new array which is gonna store the users because I want to filter through this array when we are actually deleting a user so I'm going to save our users it's gonna be equal to user 1 which we created right here also user 2 like so ok so we have those two users in this arena now what I'd like to do inside this delete user method right here is passing the user that I want to delete so when I call this delete user method we're going to pass in as an argument the user we want to delete right then what we're going to say is we want to update this uses array because we're going to delete one of them out of it we're going to fill some one out of it so we'll say users is equal to users dot filter now the filter method in JavaScript allows us to cycle through each element inside the array and then filter one of them or more of them out of the array now each time we cycle through the array we get access to the individual item so what we can do here is pass individual item as a parameter to this es6 error function so we're taking that individual item inside this function now and we can check if that user this thing right here is equal to the user that we've passed in if it is equal then we want to delete that user from the array we want to filter it out now inside this function if we return false for a particular user it's going to remove that user from the array if we return true then it's going to keep that user inside the array so what we need to do is say return either true or false and we're going to say you email which is the email property of whichever user we're cycling through at the time okay and if that is not equal to user email then this is going to be true right so we're cycling through the array if the email of this one is not equal to the email right here then this is going to be true and we're going to keep that user in the array if the email of this one is equal to the email of this thing right here this is now going to be false therefore we're returning false we're going to filter that out of the array now okay so then now we have that filter method the next thing that we need to do is actually create an admin we don't have an admin yet so let's do that well save our I'm just gonna call this admin you can call it what you want on this time we say not new user but new admin all right so now we're creating this new admin class we still need to pass in an email and a name because we're still calling this constructor function right because we're saying that admin extends from user so we're still expecting all of this stuff right here so let's pass that in so the name is gonna be Shawn at ninjas comm and then I'll that's the email rather the name is gonna be Shawn okay then so we have this admin them so now let's say okay admin dots delete user and the user is gonna be user - all right so I'm gonna try and delete that now from this array so what down here is in fact we'll need to call this after this array has been defined and then underneath that I'm going to console dot log users just to see if it has in fact worked so let's save this now and run it and now we can see that uses just has one use inside of it so we've removed user 2 now what I want to do also is add in admin right here because at the end of the day this admin is still a user right this time or delete user 1 so we're deleting array of this time so let's save that see if this works okay so now we still just have to so we've deleted one of the users one of them still remains I'm the admin and you can see Yoshi remains and Shawn and ninjas calm remains as well so we have now deleted user 1 which is right right here so cool so what we've done here is create a new class called admin which is inherited from the user class it's inherited all of this stuff right here and it can demonstrate that because I could say admin dot log in like so and that's still gonna work right if we say admin then we're still going to see down here that inside the proto we have delete user because we have that on the admin class but if we go into proto again and we'll explain what these protocol things are later on in the course then we can see we've inherited these two functions or rather these three functions as well which is on the user class alright so that's how we inherit things in separate classes now obviously if we try to do something like this I'm going to comment out this if we try to say user one delete user and passing user two then this shouldn't work because we're not giving ordinary users this delete user method they shouldn't have access to that that's only for admins right here so let's save it and make sure that this doesn't work and you can see user 1 delete user is not a function so that's cool we're just extending the user and giving admins only this extra method right here
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 57 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
▶
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: AI Pair Programming
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
7 Best AI Tools for Research, Coding, and Development in 2026
Medium · Data Science
7 Best AI Tools for Research, Coding, and Development in 2026
Medium · Programming
How to Write a Project Status Report With AI in 15 Minutes
Medium · AI
7 AI Tools That Can Save You Hours Every Week
Medium · AI
🎓
Tutor Explanation
DeepCamp AI