在TypeScript中使用private [英] The use of private in TypeScript

查看:407
本文介绍了在TypeScript中使用private的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是征求意见的要求,我们称其为RFO .我希望社区对此表示欢迎,因为我非常希望获得反馈,并认为总体上我们都可以在此方面使用更多见解这个经常被忽略的问题.

This is a request for opinion, let's call it an RFO I hope this is received well by the community as I very much would like the feedback and feel overall we could all use some more insight on this often ignored issue.

自C#问世以来,我一直是C#开发人员(我从ASP过渡过来).在编写代码时,我自然会使用publicprivate.即使在默认情况下,也无法阻止我在TypeScript中将public添加到我的方法和属性中.

I have been a C# developer since C# came out (I transitioned in from ASP). I have a natural tendency to use public and private when I write code. It's about impossible to stop me from adding public to my methods and properties in TypeScript even with it being default.

我对TypeScript还是陌生的,甚至还不是认真的JavaScript(在打字稿上工作了2年).

I am somewhat new to TypeScript, or even serious JavaScript (2 years in on typescript).

众所周知,private实际上在转码后不是不是 private.因此,尽管private 成员访问权限对于单独或团队工作的Typescript开发人员是有帮助的,但输出JavaScript却被多余的变量所污染.

As we all know, private is in fact not private in the transpiled code. Therefore, while private member access is helpful to Typescript developers working alone or in a team, the output JavaScript is polluted with extra variables.

可能会有吸气剂和精美的设计原理,但是最后,您所拥有的是一堆变量,它们执行相同的操作,或者至少抵消了我们所有保护工作的努力代码.我们使用下划线_toLetOthersKnowOurIntent. Angular的样式指南建议我们避免在私有变量上使用_.

One may have getters and beautiful design principles, but in the end, what you have is a wall of variables that perform the same action, or at the least, negate all of our efforts to protect code. We use the underscore _toLetOthersKnowOurIntent. Angular's Style Guide suggest we avoid using _ on private variables.

然后的问题是,如果您正在使用打字稿编写代码以供一个人使用JavaScript(某种包装)使用,那么使用私有属性是否会更加有害且令人困惑.使用或private仅适合与其他TypeScript开发人员一起使用.如果您正在开发一个库,说要通过NPM分发一个Angular组件,应该避免同时使用private吗?

The question is then, if you are writing code in typescript that is to be consumed by one using JavaScript (a package of some sort) is it more harmful and confusing to use private properties. Is the use or private only good for working with other TypeScript developers. If you are developing a library, say an Angular component to be distributed via NPM should one just avoid private all together?

推荐答案

TypeScript类型和可见性功能不会影响JavaScript输出.

TypeScript types and visibility features don't affect JavaScript output.

由于TypeScript是Angular社区的首选语言,因此通常会在以TypeScript编写的库中尊重私有成员.

Since TypeScript is the language of choice in Angular community, private members will generally be respected in a library that was written in TypeScript.

角度样式指南是自以为是的,并不一定要考虑所有因素. TypeScript可见性反映在生成的文档和IDE中,但是使用额外的方式来指定成员不公开总是有好处的,例如在调试过程中,或在JavaScript中使用已编译的TypeScript代码.

Angular style guide is opinionated and doesn't necessarily take all considerations into account. TypeScript visibility is reflected in generated documentation and IDE, but it is always beneficial to have extra way to designate that a member isn't public, e.g. during debugging or using compiled TypeScript code with JavaScript.

Angular代码库本身包含许多_下划线的成员,仅因为这很方便,所以显然不认为官方样式指南是无懈可击的.您唯一遗憾的是何时需要进行销毁:

Angular code base itself contains a lot of _ underscored members, just because this is convenient, so official style guide clearly isn't considered impeccable. The only time you regret is when there's a need for destructuring:

const { foo, bar } = this; // nice
const { _baz: baz, _qux: qux } = this; // not so nice

另一个问题是,并不是每个IDE或编辑器都完全支持Angular模板.由于AOT编译的工作原理,它要求组件模板中使用的所有成员都是公开的.这不会影响通常用于开发的JIT编译.确保私有成员不会在模板中被意外使用,这是开发人员的责任.这是_匈牙利表示法解决的另一个问题.

Another concern is that not every IDE or editor has full support for Angular templates. Due to how AOT compilation works, it requires all members that are used in component template to be public. This won't affect JIT compilation, which is usually used for development. Making sure that private members won't be accidentally used in template becomes developer's responsibility. This is another problem that _ Hungarian notation solves.

如果用户有可能从扩展课程中受益,请考虑使用protected而不是private. JavaScript中没有真正的封装,因此不适当的可见性永远不会阻止用户按照用户想要的方式使用第三方代码,而只会绕过圈.

If there's a chance that a user can benefit from extending a class, consider using protected instead of private. There is no true encapsulation in JavaScript, so unsuitable visibility will never prevent a user from using third-party code the way a user wants but will just make jump through hoops.

这篇关于在TypeScript中使用private的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆