打字稿类:显式“公共"修饰符是最佳实践吗? [英] Typescript classes: Is explicit 'public' modifier a best-practice?
问题描述
在 TS 中,类成员的默认访问级别是 public
,除非指定了其他任何内容.即便如此,无论如何使用 public
修饰符是否被认为是最佳实践?如果没有别的使代码更明显?
In TS, the default access level for a class member is public
unless anything else is specified. Even so, is it considered a best-practice to use the public
modifier anyway? If nothing else to make the code more obvious?
推荐答案
IMO,这是一个主观性很强的话题,没有完美的答案.但是,我认为确定答案的一个重要因素是您是否并行使用其他语言,以及 TypeScript 和其他语言之间的默认访问器修饰符是否存在差异.
This is a strongly subjective topic to which no perfect answer exists, IMO. However, I'd say a strong factor in settling on an answer is whether you are using other languages in parallel, and if there is a difference in default accessor modifiers between TypeScript and those other languages.
以 C# 为例.在 C# 中,没有显式访问修饰符的每个属性和字段都是私有的.在 TypeScript 中,它显然是公开的.
Take C#, for example. In C#, every property and field without an explicit access modifier is private. In TypeScript it's public, obviously.
如果您碰巧在同一个项目中使用 C# 和 TypeScript,或者只是并行使用,我建议您使用显式访问修饰符,只是为了清楚起见.
If you happen to be using C# and TypeScript in the same project, or just in parallel, I would recommend going with explicit access modifiers, just for the sake of clarity.
这篇关于打字稿类:显式“公共"修饰符是最佳实践吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!