This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.

Check out a free preview of the full TypeScript Fundamentals course:
The "Access Modifier Keywords" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike discusses access modifier keywords, public, protected, and private, which help encapsulate a class and determines access to the class. Mike takes questions from students.

Get Unlimited Access Now

Transcript from the "Access Modifier Keywords" Lesson

>> Mike North: Two more concepts I wanna teach you before we jump into the next exercise. So, these are Access Modifier Keywords. These can be used with member data or member functions on a class. And these mean the same things that they mean in other programming languages. By default, everything is public, right?

[00:00:23] Meaning that if I create an instance of account, I can access account ID, and I could, even if we had no access modifier keywords here. I could say new account and then, if I did A equals new account, I could do A dot account ID and I'd get the value back out.

[00:00:41] Protected means that only Account and subclasses of Account can access that data. So, you can see that email for this SharedAccount, whatever that means, and the setEmail string, it is free to use this protected value. Private means only Account can have access to it, only that class, subclasses don't even get to see it.

>> Mike North: There are two other modifier keywords, one we've seen already, which is static, and another which we'll see a little bit of tomorrow and I'll clue you into it, read only. So if you say read only it protected email string or something like that now, once it is set, it’s effectively like a.

>> Student: [INAUDIBLE]
>> Mike North: Pardon?
>> Student: [INAUDIBLE] do get length or whatever?
>> Mike North: So, the difference there is if you only have an accessor a mutation will have no effect. So you can attempt an assignment.
>> Student: Okay.
>> Mike North: And it'll be like okay, sure, whatever. And no value will be changed.

[00:01:56] Now, if you use an object like a property descriptor, right? It's like object.define property and there's an ability to set on the property descriptor a read only attribute. And there you would end up with a run time error if you ever try to write that thing.
>> Mike North: And that's what this read only means by the way.