What is the purpose of the public access modifier for classes in Typescript?

前端 未结 3 397
南方客
南方客 2021-01-20 17:38

What is the purpose of the public access modifier for classes in Typescript?

What is the difference between

export class Thing {

  public doStuff(){         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 18:23

    It's just an explicit way to set the method to be public. By default all class methods / properties in TypeScript are of type public, but you can as well note this in your code for clarity.

    You can read more about it in the Handbook, here is an excerpt :

    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.

提交回复
热议问题