Public by default
In our examples, we’ve been able to freely access the members that we declared throughout our programs. If you’re familiar with classes in other languages, you may have noticed in the above examples we haven’t had to use the word public
to accomplish this; for instance, C# requires that each member be explicitly labeled public
to be visible. In TypeScript, each member is public
by default.
You may still mark a member public
explicitly. We could have written the Animal
class from the previous section in the following way:
class Animal { public name: string; public constructor(theName: string) { this.name = theName; } public move(distanceInMeters: number) { console.log(`${this.name} moved ${distanceInMeters}m.`); } }
Please login to continue.