Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:14Length:1.2 hours
Typescript fundamentals 400x277
  • Overview
  • Transcript

2.4 Access Modifiers

It's important to control access to properties and functions. That's the basis for a clean API interface. In this lesson, you will learn which modifiers allow and deny access.

2.4 Access Modifiers

Hi, and welcome back to TypeScript Fundamentals. In this lesson you will learn what access modifiers are and how you can use them. Normally, when you have a class, you can define parameters and everyone who uses objects of this class can read them. Sometimes that's not what you want because they might be secret, or you want to control the way consumers use your class. That's where access modifiers come in handy. They allow you to limit access to your parameters or functions in a class. TypeScript has four different types of modifiers. By default, everything's public, you don't need to explicitly state that, but most times it makes sense to clarify it for yourself or others. If you don't want anybody outside of the class to access a property you define it as private. Now they have to use a controlled way, if there is one, to access it. Unfortunately, this also means that classes inheriting from your base class can't access the variable either. That's a job for protected. They still protect from outside access, but allow deriving classes to use the property. Sometimes it's okay if people can access a property, but making it public would also mean that they can modify it, which you might not want. For this use case, there's the read-only modifier. This way you can get the value, but can't set it. Read-only properties, however, must be initialized either in the constructor or during the declaration. So they might not be as useful as you would like them to be. For this, at least when you're using ECMAScript 5 or higher as a target, there is a solution. You can use get and set to make it feel like something is a property. This avoids ugly functions like get story or set story. If you don't want the private property to be changed from outside, you simply don't provide a set function for it. One note about the wording though, the function name has to be the same as the virtual property you are creating. If you need to store something in a similar variable, use an underscore before the name. When using clases you sometimes want to call a function on the class itself, and not its instance. For example, when you are trying to create an instance through it. Sometimes such structures are necessary. And you might want to limit who can instantiate a class. You can apply the same access modifiers to constructor as you can to everything else. If you set a constructor to be protected for instance, nobody outside the class can instantiate it anymore. They have to go through static functions. There you have it. Access modifiers are important to limit access to properties and functions, as the name says. They are essential to creating a clean to use interface that makes sure the integrity of the data and behavior inside the class is not compromised. To recap, you can use public, private, protected, and read-only to modify access and behavior of a property or function. When targeting ECMAScript 5, or higher, you can also create getter and setter functions to simulate a property to the outside. Static functions don't get called on the instance, but on the class itself, and have the same access rights like regular functions inside a class. You can make constructors private if you don't want the class to be instantiated and unsupervised. Static functions still have access to the constructor. In the next lesson we are going to look into organizing your code with modules and namespaces, see you there.

Back to the top
Continue watching with Elements