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
Original source
23 responses to “Object Oriented JavaScript Tutorial #8 – Class Inheritance”
Inheritance does seem useful, but I have worries. What if, later on, you need to add properties to the user class that admin does not require?
I try:
admin.deleteUser(admin)
but it's not work. Can you explain why?
Your tutorial is really amazing. keep up the good work
I wasn't able to chain the delete admin method with a console log. anyone any
idea why?
you should have explained super()
Easy and simple explanation of js
keep up the good work
Can you please describe why did you use array.filter() instead of array.find()?
King Of Ninjas, Thanks For These Tuts They're Very Helpful, But I Hope You Make A Small Project Using Javascript ES6 OOP, Like OOP Shapes, Wisky Machine, etc … 🙂
Hello , i was practicing after the video , i wanna know can i put the filter() function in separate function for example deletion() then call it in the deleteUser() function like that : users = users.filter(deletion);
After that deleteuser method why you passed user not users
cool. you are the best! thx!
admin.deleteUser(admin) ☠️
What is theme name are using?
Hey Ninja i wouldlike it if you revisted this topic thank you
admin.deleteUser IS NOT A FUNCTION
class User{
// inside the class create a constructor to help us create object instances
constructor(name, email, score){
//set constructor properties
this.name = name;
this.email = email;
this.score = 0;
}
//class methods are written outside the constructor but inside the class
login(){
console.log(this.email, 'Welcome …….loggedIn');
return this;
}
logout(){
console.log(this.email, 'Bye Now……..loggedOut');
return this;
}
updateScore(){
this.score++
console.log(this.email, 'Score is Now', this.score);
return this;
}
}
class Admin extends User{
deleteUser(user){
users = users.filter(u => {
return u.email != user.email
});
}
}
var userOne = new User('Nelson','nelson@gmail.com');
var userTwo = new User('machoka','machoka@gmail.com');
var userThree = new User('momo','momo@gmail.com');
var admin = new User('mekenzi','mekenzi@gmail.com');
//delete user
var users = [userOne, userTwo, userThree];
admin.deleteUser(userOne);
console.log(users);
You deleted userOne only from array users., userOne exists henceforth.
I am the 1000th viewer yayyyy!!!
class Admin extends User{
deleteUser(user){
users = users.filter(u => {
return u.email != user.email;
});
}
}
TypeError: Admin.deleteUser is not a function
why??
Why you're using the var keyword instead of let or const?
Salut et merci 🙂
Today, I have a little question:
Why do we see [User, User, Admin] (class names) when we make a console.log (users) instead of [userOne, userTwo, admin] (the names of the variables)?
The names of the variables no longer appear, even when we are doing 'users [0]' in the console.
Thanks in advance
Super helpful, thanks a ton Shaun! Being a newbie, I do have a general question about the code here. The deleteUser() method seems to only except the user as an argument instead of the user and users even though the method needs both to work. I'm assuming that's because the users array in global scope? Also, would there be any benefit/best practice to explicitly require the users array as an argument? e.g deleteUser(user, users)
i would loved if you make "Real world projects" based tutorial on Udemy using JavaScript/Nodejs/ReactJs technologies 🙂
"Inherit"(or "extends") is simple and useful. Your explanation is the best as well. Thanks!