Exercise - Apply access modifiers to a class
In this exercise, you'll apply access modifiers to properties and methods of the Car
class.
Continue defining the
Car
class in the Playground.Test the access of the class members by typing
myCar1.
and notice that all the members appear in the list, including the properties, theconstructor
parameters, the methods, and theworker
function.Set the access modifier of the
_color
,_doors
, and_make
properties and theworker
function toprivate
.// Properties private _make: string; private _color: string; private _doors: number; // ... private worker(): string { return this._make; }
Test the access of the class members again by typing
myCar1.
and notice that the properties and theworker
function are now unavailable. Any attempt to use these class members will raise an error at compile time.