Understanding “public” / “private” in typescript class

前端 未结 3 893
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 15:22

In the below type script code , irrespective of whether name is \"public\" or \"private\" , java script code that is generated is same.

So my question is, how to decide

3条回答
  •  广开言路
    2021-02-05 15:27

    java script code that is generated is same

    They produce the same JavaScript but don't have the same semantics as far as the type is concerned.

    The private member can only be accessed from inside the class whereas public can be excessed externally.

    More

    The differences are covered here : https://basarat.gitbooks.io/typescript/content/docs/classes.html#access-modifiers

    Another example

    let foo = 123;
    

    will generate the same ES5 as

    const foo = 123; 
    

    However in the first case let foo = 123;foo = 456 will compile fine but const foo = 123; foo = 456 will result in a compile time error.

提交回复
热议问题