OOP with TypeScript: Constructor Functions w. Inheritance

TypeScript
Transcript

English (Auto-generated)

uh a little more about the constructor function. Actually, we know how we can manage the constructor function, how we can use it as an initialize. Er But there is one more thing, for example, we have another another class. I'm going to create another class right now, the class and space again, I'm I'm creating the student class. I just created an a student class and I'm expanding it extends to person, person. And here um when I extend it to person, I'm going inside and you know, this is just the class, the student class which has which should have everything we have in the um in the person class, actually in this in this class, but because we initialize the data this way, so what we are going to do for this, I mean, you know, let's say we just created a class student which has everything this um this class has. So if I just go down, Let's say I'm gonna create an object, let S1 equal to new spaces students okay. And that's how we just connect the class. Okay. We are we are having a problem. A little problem. So um or how we can deal with this thing. The first thing is um you know, I'm gonna go there and I'm gonna create another constructor function. And what I'm exactly gonna do um we do not need that, just make it silent. Okay. And now this is a constructor function. So when we were extending the person class to this student class. So the first thing we need to do is to get everything we need here as the um right here, we need to write it full name. The second thing is the age as it is. Okay. And I'm gonna go down and I'm gonna call the super keyword as you know, to access the data from the from the parent class. We need to use the super keyword. And inside this super keyword I'm gonna call full name, comma, comma. Age. Okay. I've called both of these things. Has any type. Okay, let's define it. Type. It has to be the string and it has to be the number. Okay, now the full name. The full name and age. So now what we did here, we actually created a class student which is extended by the person class and then in the constructor function which we created for this uh no class, we called it and we got the data, we have to initialize the data we need uh in this class. So now the full name and the age is here and we use the super keyword and the full name and the age is also here. And absolutely because of the super. Now you got it right inside. Now we simply need to call the object wherever we want to and now you can see that and an argument. Full name was not provided. So I'm going inside and I'm gonna call, I'm going to give the data now I'm talking about the students, I'm writing let's say um a male Emma comma. And then the 27 I'm saving it and I'm going down and I'm calling the console dot log And inside that as one and saving it. Okay now I need to go to the browser and I need to write the TSC and after that the nodes pay string. Sorry script dot Js. And I'm gonna get the person full name John 26 student full name Emma. Age 27. Okay. So I think this is self explanatory simply we just created a constructor and we simply imported both of them here and then we just simply, you know, call the data using the super. I just called them here from the above class. So absolutely. When we put the data here and this data will go to this these values. Let's see if I write F name here or age, um age and here. So you can see that it's not now, you know um Exactly working the way it was working previously because you know the full name, this start full name. So it's it now creates the complexity because the values the name of the parameters has to be the same as this. Super. Super values. I mean the the real ones we're coming we're getting from the above. Okay. So this is simplified now um let's say we want to create a separate um property like the grade here and the grade is isn't it strange. Okay. So I just created a new one. The grade. Now there is one way we can initialize this grade right after the declaration of the object as we did it previously. I mean we know that but we can call it the great inside the constructor function to by adding a comma and by simply calling the grade and um string. Okay now what is happening here? This grade is not coming from the above. So this grade cannot simply be called inside the super. I need to go after the super keyword and I need to call this dot great equal to grade. Okay now this is simple. You know, we as we get here, you know, we call the F. Name or the age and then we simply call this dot full name equal to age. So similarly similarly the grade um They start grade is equal to this grade. That grade. Actually this grade means I'm talking about this grade and this grade should be equal to that grade. And that grade is gonna gonna have some value from um from wherever it's going to be called. Okay, so now we see the constructor functions structure. The first parameter is the full name. So absolutely. This is the full name. The second parameter is the age which is the number type. So this has to be a number and it can be anything and then add a comma. And now I'm gonna add the grade. The grade can be anything like but it has to be the string. I'm writing the B plus. Let's say okay now I'm just going there and you know I simply call the students here and now I'm simply going there and what more I'm gonna do is um TSC and the nodes script changes and I'm running that you can see that the full the person full name john and the Ages 26 here the person full name is Emma. Ages 27 the greatest B plus. So this sounds um understandable that you can add your own data using the constructor function and how you can pull up, you know the above data. And you can see that how things actually works with the extended. Uh When you you extend the class or inherit the previous class with a new one. Uh and use the constructor function. So in the next video we're surely going to talk about something more till then. Did you
140 Views 0 Likes 0 Comments

Let's dig into the concept of inheritance within constructor functions.

Comment
Leave a comment (supports markdown format)