JavaScript Classes #4: Inheritance & Extends - JavaScript OOP Tutorial
Вставка
- Опубліковано 1 чер 2018
- By using the "extends" keyword with your JavaScript classes you are able to create inheritance which is where you have a child class that inherits the instance properties and methods of its parent class.
You'd typically use a feature like this on the server-side with NodeJS as this can be considered to be a complex design - although classes can be used to represent similar visual components on the front-end.
In this video I take you through a simple example of how you can make use of the extends keyword to achieve inheritance - you'll see how we create a "child" version of a Person class, a Programmer. programmer has all the features of a person but with a little but extra!
For your reference, check this out:
developer.mozilla.org/en-US/d...
Support me on Patreon:
/ dcode
Follow me on Twitter @dcode!
If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Your way not to hide occasional typing mistakes and executin errors is the better teaching method than showing perfect procedures. I like the way.
mesmo sem entender o que ele estava dizendo,suas separações e jeito de fazer me deram uma luz,obrigado!!!
Always. Simple clear. Love your teachings
Great explanations, from the start i understand what i didnt understood before
Wow! Amazing explanation and examples. Thank you!
Great explanation, thank for this video.
thanks, was really confused about the super method, think i got it now!
Hiya mate, don't mind me just saying how much I love this series again lol
Great explication,Thanks very much!
Thank you, it was a really helpful video! :)
Thank you for the clear explanation.
You're very welcome!
you are really teach thanks so much
Thanks, Sir.
thank you man
Hi Dom, thank you for this tutorial. I'm wondering if "extends" is somehow similar to Object.prototype?
Thank you very much for the informative video! You have a knack for teaching. I had one question though; when you form the class's instance properties:
class Person {
constructor(_name, _age) {
this.name = _name;
this.age = _age;
}
Would the correct syntax to indicate a private property be this instead?
class Person {
constructor(name, age) {
this._name = name;
this._age = age;
}
Since that way, once the actual object instance is seen with the property name, it becomes more apparent that it's a private property?
Really small detail, but just wasn't sure what the so-called "standard" should be. Thank you again for a great video!
I love your wpm. Loved listening to your keys.
BTW Thanks for making this video.
Does super(_name, _age) being the name and age properties to the new child class. Am I right in my understanding that once you have used super(_name,_age) you save time by not writing those properties yourself and just "extend" it from another class?. If the parent class does not have a property you need, you can add it yourself like you did with yearsOfExperience.
Hey there, calling "super(_name, _age)" will call the parent constructor, meaning whatever code is in the parent constructor will be executed for the context of the child class
I also have another question for you, sorry for taking your time. can we inherit multiple parent classes in our one child class?
If it can be done, how do I do it?
I also have other question, sorry for taking your time. The only way to access the property or method of a child class is by using its own name and not its parents. Is this correct?
Yeah that's right, you can only call the method on an instance of a class where it exists
I dont understand this too
const programmers = [
new Programmer("Dom", 54, 12),
new Programmer('Jeff', 24, 4)
] I know its an array but how do you call a class from inside an array, how does this code work?
When you call a constructor you'll get an instance of the class. So inside the array you are calling two constructors so you'll have two instances of that class inside the array.
This could be another way of achieving the same result:
const P1 = new Programmer("Dom", 54, 12);
const P2 = new Programmer("Jeff", 24, 4);
const programmers = [P1, P2];
In your comment example they are called p1 and p2, but in the video they referenced as programmers[0] and programmers[1] is this correct?
I was just showing you another way to achieve the same result :) you can then reference the two Programmer instances by using programmers[0] and programmers[1]
pazira love, I think it wouldn't be correct to say he "calls class" from inside of array. What does call class really mean anyway? He creates new instances of Programmer class and stores them in an array called programmers. And in the last part, our function uses this array, it loops through it and makes them work ))
Is this Js Es6 Or normal Js?
Hey there mate, this class syntax was released with ES6 😁
dcode I'm begginer so little bit confused anyway thanks
when you realise a 10 minutes youtube video nowadays worth more than a whole semester of your college smh.. 😤
Nice video, thanks! Though, please use some girls' names for your examples. Not only men code ;)
Are u 56?