如何选择私有成员和let绑定?
当编写不需要访问同一个类的其他成员的私有方法时,如何在私有成员和let绑定之间进行选择?
- 我倾向于使用私有成员,因为如果需要的话更容易更改可访问性,但是在做出选择时还需要记住其他方面吗?
- let 绑定是否编译为私有成员(这只是一种样式选择)?
When writing a private method that has no need to access other members of the same class, how do you choose between private member and let binding?
- I tend to use private members because it's easier to change accessibility if required, but are there other aspects to keep in mind when making the choice?
- Are let bindings compiled as private members (making this only a style choice)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
规范的相关部分是 8.6 节.2。它指出:
还:
我更喜欢对私有成员进行
let
绑定,因为它们更具“功能性”,即它们强调“什么”而不是“如何”。编译器负责优化编译形式。The relevant portion of the spec is section 8.6.2. It states:
Also:
I prefer
let
bindings to private members because they're more "functional," i.e., they emphasize "what" over "how." The compiler takes care of the optimal compiled form.类中的
let
绑定是私有的。我认为let
和私有member
之间的主要区别是let
绑定不能重载,并且使用name( )
而不是this.Name()
。因此,我认为这主要是一种风格选择。let
bindings in a class are private. The main difference I think of betweenlet
and a privatemember
are thatlet
bindings cannot be overloaded, and are called withname()
rather thanthis.Name()
. As such, I think it's a mostly stylistic choice.let
绑定无法通过类实例访问,但private 方法
可以。例如:let
bindings cannot be accessed through the class instance butprivate method
can. For examples: