OOP with TypeScript: Member Visibility

TypeScript
Transcript

English (Auto-generated)

talk about the Members visibility. So what are the members actually um, you know, this property and that is a property, the full name and the age. Both our properties. So these are the members of this class. And if we have any method and that would also be called a member. Ultimately, the constructor function is also a member, but this function doesn't have any name. So it's, you can say, you know, it's a base function. So we just cannot call it or we just cannot talk about the visibility of this function because whatever it does, we do with this. The person class. Excuse me, I'm sorry. Anyway, whatever he does with this person class ultimately reflects with dysfunction, uh, the constructor function. So that's kind of understandable until now. Okay, So, so we can, you know, we can make this this member private protected or public. Let's just create a new member. A method, actually. So let's say it's and we're talking about a person absolutely eat. So we're just simply adding to control the log and uh person eats. Okay, that's understandable. Now, what is happening exactly what is exactly happening here is this is a member. This is a member. And that's a member. So three members here to members are the fields or the properties. And the third one is a method. So what I'm gonna do is I'm gonna call it public. Okay, I'm gonna call this one public. Okay? And I'm gonna call this one public. Okay. So actually, when you want to make them public, actually, this doesn't make any change because the by default value is public. When you don't provide the, when you don't define the the visibility of a member, it's ultimately public, so it's naturally or by default public. So that's, I mean, it's gonna work similarly. So let's just talk about the public's, you know, the way the public behaves a public method or the property, I mean, member behaves. So the thing is because it's public and it's public. So, um here, you can see that you can you can simply access that, and when you write, you know, P one, um you know, here we wrote P one for now, I'm just simply making all the S one content silent for a little while, because I'm talking about the P one, uh the person. So let's just make it the person that full name, and I'm just making it easier. I'm deleting back to. Okay, so that's a class we've defined and that's here. We simply create, created an object and we assigned the object, we assign the class person, class to this object, and then we called the p one dot full name. I mean this value. So I'm gonna go there and I'm gonna write the PSC and then I'm gonna just make it, you know, node script dot Js I'm gonna have this myth. So this means it's a public, it's the public one. If I make it private and I save it now, you can see that you just simply, you know, you just cannot call it. You know you can see that the property full name is private and only accessible within class person. What does that means? You still can utilize this property full name but you have to be inside this person class. You just simply cannot call it outside. And that's that's one of the wonderful functions. Maybe for example um let's say um you want to you know you actually have done similar things in the previous previous part of the um the O. P. The previous series. So um where we talked about the methods originally so I'm just gonna I'm just gonna create something like you know another public function here. Public function. Okay and in this function what I'm gonna oh sorry I just simply wrote the function. That is absolutely not good. So let's just call it public and then um be fine dinner let's just say I mean okay and then it makes things easier. Okay now I'm doing one more thing here, I'm just simply writing I'm actually making this one as public public and I'm creating another one here. I'm creating another one which is private and this is general and it has to be this string and absolutely this cannot be called outside but maybe we need that. So we're just going there and um what we're gonna do here is um right here general defined Jenner or I can directly get there because I already have the data this dot um generally I mean we're getting the data of the general is Jenner equal stew now we are accessing this value and let's say we're calling it for now we're calling it meal. I'm not getting the data from somewhere from here but absolutely we need to make two functions. The getter function on the set of function and we already had a talk about this, this is a set of function and we can absolutely can create a setter getter function too. So for now I'm just or let's just create the getter function to creating the getter function right here um right here public um yet Jenner Okay. And return and what we're going to return is this dot Jenner Okay. That's understandable. And hear what I'm doing. I'm writing Jenner. Okay. And this has to be general why this is having the problem because any okay we need to define the type and the type of the string. Okay this is a central function and that's a ghetto function and this is how we can access this data out of the class because by default we just cannot access the private classes. So anyway um uh the you know, first of all we need to define general and then we can get the general so right here we have to call this public function. I mean like here um we have to call this public function. So p one dot get general and we have to provide with some value and the value. Sorry, we just cannot use the get general, we need to use defined general and now we need to provide some value like uh you know because it's a parameter so we need to provide some value like you know meal because it's a smith, so I just provide the value meal and then I'm gonna go down and I simply I'm gonna call I'm gonna call you know here I'm gonna call get Jenner and I'm gonna save it Okay let's call it. Okay, I'm gonna save it, I'm gonna go there TSC and then the node script dot Js I got the meal now what is the thing? Um you know we just we just access the private General but you know, we just directly cannot access the private general, out of the person's class. I mean we just cannot call this directly. So how we did it we used together and the setter function, we can I think we can delete that because it's not necessary anymore. So we just use this getter and the setter function and which we have talked about in the previous series anyway. So in the central function we simply called the the general and we because we can call this inside inside the class and we did it the value. And then in the get general function, we simply called it. We just returned whatever value we set for this general and here we simply called. We we didn't call the private private member but we call the public member which is this and throw that. We just got the data. So I think the public and the private is ex explained. Well, there is one more concept which is protected and the protected works with the inherited or the extended classes. So we're going to talk about that in the next video until then stay tuned.
170 Views 0 Likes 0 Comments

What exactly is member visibility?

Comment
Leave a comment (supports markdown format)