TypeScript Tutorial #13 - Public, Private & Readonly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay then gang so in the last video we created this class right here and we could access or change all of the different properties on class instances after we've created those right here and I'm going to demonstrate that by cycling through each one of those using a for each loop because we have two invoices in this array currently invoice one and invite two which are here in here and I'm cycling through each one of those inside that array using a for each loop and I'm referring to each one as an info invoice and logging out the client property the details the amount and also invoking the format function as well and logging that to the console which is this string right here so what I'm going to do is save that and come over here and we can see for each one we get Mario we get work on the Mario website that's the detail so client details amounts to 50 and then we get the former sentence at the end we get the same for this one Luigi which is the clients this which is the details this which is the amount and then this which is the result of the format function so we're able to access all of those and if we wanted to we could change those as well because we're saying right here that anyone can access or change those so that's the default behavior of classes all properties are public by default and it's the same as putting something like public in front of it now this is an access modifier and we can use those to modify how a user can access different properties in a class so this right here is saying look this is going to be public and you can change it you can access it I don't mind now that is the default behavior right there so we don't need to explicitly say it but we can do if we want to just to say this is public now if you want to make something private you can do by saying private in front of it now you see we get an error down here because now we can't log this to the console it's private we can't access it when we create a new one of these things right here a new instance we can't then access that property from outside of the class itself now we also refer to the details down here we say these details but this is inside the class itself and when we have a private property we can access that property inside the class using this method but we can't access it from outside of the class on the instance directly so this is not going to compile on them so I've made that private so let me delete this because we can't use that anymore and let me save this again and you'll see that we can still access it via the format method because this still outputs the details right here it's just that we're not accessing it directly from outside of the class so let me save that and preview and we can still see that we get the details printed right here so that still works the other access modifier is the read-only one and this means that we can read this from outside and also inside the class itself but we can't change it so for example if I try to now say down here that in doc client is equal to something else then it won't let me do that we get an error because this is read-only but we still can access it to read it now the same is true inside the class itself if I wanted to change this I still can't do that inside the class I can't say this doc client is now equal to something else we can't do that because this is marked as read-only so they're the three different modifiers we have we have public which is the default one means we can change and read the value outside of the class as well as inside of the class the private one only allows us to read and change inside the class and the read-only one only allows us to read it both inside the class and outside the class but not change the value okay now then I wanted to show you a shorthand for all of this right here because right now we're defining these properties then we're taking in three parameters and we're assigning those values to these different properties right here now there is a quick way to do that so let me just comment out all of this and this is by the way only when you're using access modifiers in front of the different booty's so let me comment this out and let me delete this and move this up here and what I'm going to do is also delete these now we have three properties that we want to define right we have the client the details and the amount now I can define all of those inside this constructor in the parentheses right here and automatically assign the values that we pass in to those properties by saying for example and I'm just going to go on to the next line to do this so it's formatted a bit better read-only client and that is going to be a string and comma then private details and that is also going to be a string comment and then public amount which is going to be a number okay so now this only works if you're using these access modifiers in front of these different parameters or properties it's not going to work if we don't have these access modifiers there but now when we're passing these values to the constructor it automatically assigns those values to properties of these names to the class so now we don't need this stuff at the top okay so let me save that and make sure everything still works yep everything still works it still assigns those property values but if we take away these things right here then it's not going to assign those right so we have to put those modifiers in front of the different property names so let me just undo all that and save and there we go everything still works
Info
Channel: Net Ninja
Views: 92,472
Rating: undefined out of 5
Keywords: typescript, typescript tutorial, typescript vs javascript, ts, ts vs js, typescript for beginners, tutorial, tutorial for beginners, typescript tutorial for beginners, what is typescript, typescript basics, install typescript
Id: aYmnwDlPB8s
Channel Id: undefined
Length: 6min 6sec (366 seconds)
Published: Tue May 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.