OOP with TypeScript: Constructor Functions


English (Auto-generated)

constructor function in the in the GOP in the class is actually um you have to initialize your variables um when you are creating your object but here you can initialize your variables inside the class and how you can do that. Let's talk about it. So I'm gonna create a class for now and again, the class is a person. Okay. And that's how we simply create a class and now we need to go inside the class to put the other other things we need. So right here, sorry, we do not need this thing. It was creating a problem anyway, so let's say again, we're talking about the full name. Okay. And the full name is string Again, for now, I'm just going to the string. I'm not putting any more thing so string and mm hmm. In general, you just, you just go down wherever you want. Um the object, what you do is you create the object like let and you call the P one or whatever you wanna call, just as the name of the person, P one. And then you assign this like this very new dot person. Okay. And then now actually, you know, you created the object but the object is empty and you need to put the value in the object this way, P one dot full name. Okay, Sorry, this has to be this way. And then you write, you know, john or whatever you want to and then whenever you call the console the log P one and you just save it, you need to go there and you write TSC and no script dot Js and you're gonna get you can see that the person full name, john So actually we got this object here and then the object as a result, we also can call this in this manner P one dot full name. And you need, you just need to clean this up um no script dot Js and you're just gonna get the job. So okay, this is a general method but you need to initialize the this property here after calling after actually After the creation of the P one objects. So we're doing this here and now there is one more method. You just simply can do this constructor and then the it's a function. So you know the rule of the functions. Okay. And the first one, what you need to do is you need to create a simple parameter. It can be anything. For example, F name, I'm crying. I'm calling it f name for some reason, I mean, yeah, okay, this is it. And then I'm going down and right here I'm gonna call this variable. So how we're gonna call this variable? We're gonna call this by using this dot full name. This is a simple rule equal to f name. Now, what is the meaning of that? I created a method? I can create many methods like, you know, like I'm creating g just suppose it's a method. So I can, you know, simply um can can create some parameters inside the function and then we do this, we have done this multiple times, but this time it's a constructor function. So anyway, whatever the value of the constructor functions, f name would be the value of the full name. This dot. Full name. This means we're talking about the full name which is inside this class directly and that is the full name. Okay, now, um, so this way, how to call the constructor in general, if we don't have the constructor, what we would do, like we would call something like G, you know, you know, and we, we put the value of whatever we want to in this way, we don't have the G for now we have the um, the constructor function. So we, we just cannot call the constructor function because it is called whenever you're using the class or assigning the class too to any object. You you just need to go inside and you need to go this way and you need to write the name like johN Okay. And I'm just making this silent now, that is the way the constructor function works. So you just simply called it and you just got the data here and you also can do whatever you want to do with this data anyway. And now you just don't need, I mean you still can call the full name or you still can go with the key one. Let's just check what we did. No script or chairs and you just got the full name john And if I change the john from, you know, um smith smith. Okay. TSC no script dot Js. And we're gonna get the full name smith and if I call it with this way full name and I'm saving it and I'm going there back and TSC and then no script dot Js and then I'm gonna get this smith. So, um instead of initializing this after the declaration of the object by the constructor function, you can do it inside. Okay. There are a few more things. For example, you just go down and you write another one like, you know, age. Okay. And it has to be the number. Now we are going to get the problem because we didn't initialize this. Sorry. Okay. Now, if I simply go there and if I delete that and if I go there and I clear this up and I write the PSC and the node script dot Js and we're gonna get this myth only. Okay, because we simply didn't get this under the net of the constructor function. Um I can try for one thing um age equal to um 26 whatever I got in my mind, TSC no the script dot Js And now you see, you can see that we got this smith and we got the age of 26 we got the both. So this is also a way, this was not defined in the constructor function. So it was not initialized. So we can initialize it later, you can see that this is what we did, but we also can initialize this one inside the constructor function because that's the original original rule. So age and number and say that. And I'm going down in this that um age equal to and age. Okay, now this age is different from that age because this age actually this should be called this age and this should be called that age anyway. Okay, This dot age is actually referring this one and that age is actually referring um not this exactly, but this ages referring the data we need we need to receive from there. Okay. You can see that the new person, so now you can see that you're gonna have your we just got a problem because an argument for age was not provided. So we need to add a comma and we need to write something like, you know, um 45 for now. And uh okay. And I'm just gonna go to the browser back and I'm gonna clear this and I'm gonna write the PSC again and no script dot Js and oh, I got a 26 Why? Because let's just make this silent and now try again. Sorry, Okay, TSC node, it's script dot Js and then I've got the Smith and the ages 45. So Um, you know, I can individually call that too, you know, I'm gonna get the 25, so you know that already. So the thing is now, if you're not initializing after that, but we can absolutely modify it. For example, these now works as a modifier if I simply enable them and I just, you know, go to the go back there and I write TSC and I know, sorry, no script dot Js. And then you can see that the John and the age 26 because it is using now the modified values. So now I think this explains a lot and I think it helps. So this is a constructor function and you absolutely can use the constructor function to use to create the object in a one line. If you can call it a one liner um by using the constructor. So absolutely, we're gonna come back but in the next video until then stay tuned.
340 Views 1 Likes 0 Comments

Let's work with constructor functions.

Leave a comment (supports markdown format)