OOP with TypeScript: Encapsulation Part 2

TypeScript
Transcript

English (Auto-generated)

continue the encapsulation uh the encapsulation cast. We started in the previous video. So there was a tiny thing we need to talk actually I was telling it as a getter function. This is a setter function. Because the basic difference between the gate and the set is when you're setting something for example, you're setting the value here. This is the full name. The full name is actually the property we are getting from the the person. So we are getting this property. Now focus on my words were getting this property from the person. So absolutely we're not setting anything but we're getting the property. We got everything. We got every property. We got this property and all other properties from this person's to this P one from the from the person class to this P one. Okay so it's getting something. So we don't set some value when we are getting the value or property or anything. But here we are setting some value for this full name. Absolute. We're not setting anything for the person. We're setting things for the P1 because we got the properties from the person and we're setting the values for those properties here. So it is, you know we are assigning some value when we are assigning some value. We're doing um what we're doing here. We're assigning some values which means we are setting the value. So this has to be the set height And one more thing. I wrote it like this way and that way. Okay so that that is not the appropriate method. It has to be this way and it has to be this way. Let me tell you what I just did. So this height is is still the height of uh you know this I mean the the parameter. Okay and this height when I hover the mouse over this height, it's still pointing to the person dot hi that means it's pointing to this height but when I have the mouse over here it's pointing the parameters. So it's you know it's self explanatory. But the thing is we're getting the value in the parameter, we're gonna call this setter function somewhere. I mean I'm gonna show you how so we're gonna call this setter function somewhere and we're going to get the value for the height. And we're gonna we're gonna get the height here and we're gonna set the height of this height. I mean this hide absolutely by using the value of this parameter. So let's just do that. So absolutely we're not gonna go with this way because this is inappropriate and it won't work anyway. So Um we just need to call the p. one. But set when you write set, you just got to set height and you know this is a method. So you just go with this way and you need to put something some value and it's literally telling an argument for Heidi was not provided. So we need to put some value here to reflect the value um to this set function a set height function. I know it's a kind of looks like tricky but that is super simple. So I'm just gonna go there and uh I'm gonna write something like you know the height can be, it's a it's in a string. So I'm gonna write 1 68 cm and I'm going to save it. Okay. And uh um what more um the more thing is now you do not need to set, you know you do not, you actually set the height. Now what is happening above here is um you just simply set the height but you're not getting anything is still still yet. You're not getting getting anything if I just go there and I write console dot log and I write P. One. So I'm going there and I'm writing PSC and then okay, this file has to be updated by now. Yeah it is updated the seat height and everything is there. I'm gonna go back there and I'm gonna write the node um script dot Js I want to see what we did. So if I, you know you I just go this way okay now the cool cool stuff coming actually the height, we just got the height. You can see that why we're getting the height because um there is a very interesting phenomenon, you have to focus on what I'm telling now because it's kind of tricky. We assigned the person class to this object V at the data, full name age, hair color smokes. And we said that smokes true, you can see that the smoke stroke that is totally fine. But we sent a parameter to the set high center function which is 1 68 cm. But the point to be noted here we are actually what we're doing here. We actually are getting we actually, you know, whenever we are setting some method or sending some value through the parameter. So absolutely this set height represents P. One only P one. If you talk about P. Two. Absolutely. We're gonna put the height again. So we're talking about the P one and so in the P one this function is also the part of this P one and we're sending the height. And absolutely because we just cannot call the height out. I mean we just cannot set the value of height outside because it's a private one. So we just set the height of this height property using this setter function. We send the value. Why the, you know the parameter, the parameter set the value because it's a setter function. The parameter of this function set the value. The function took the value of parameter and set the value of the height here in this block of code. Now we actually, you know we cannot call this, I mean we we cannot modify this or outside. We just cannot access it, you know, but we can see the data. It's you can say kind of read only it's not actually the read only logic but you know We can see it but we just cannot access it. So when we call the whole object the P1 so we just got everything here. I think that you know that's an interesting phenomena. You might understand. You should hear this part of this cast multiple times. So if you don't understand the fact, I mean this is quite simple and quite easy and I'm gonna talk more by the next video because I know it can be a little tricky. So practice makes perfect, do practice of what I did in the previous lecture and then you would understand that and then go to the next one.
98 Views 0 Likes 0 Comments

Let's talk about encapsulation.

Comment
Leave a comment (supports markdown format)