简体   繁体   English

在TypeScript中使用private

[英]The use of private in TypeScript

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. 这是一个征求意见的建议,我们称其为RFO ,希望社区对此表示满意 ,因为我非常希望获得反馈,并认为总体上我们都可以对这个经常被忽略的问题使用更多的见解。

I have been a C# developer since C# came out (I transitioned in from ASP). 自从C#问世以来,我一直是C#开发人员(我从ASP过渡过来)。 I have a natural tendency to use public and private when I write code. 在编写代码时,我自然会使用publicprivate It's about impossible to stop me from adding public to my methods and properties in TypeScript even with it being default. 即使是默认设置,也无法阻止我在TypeScript中将public添加到我的方法和属性中。

I am somewhat new to TypeScript, or even serious JavaScript (2 years in on typescript). 我对TypeScript甚至是认真的JavaScript都不太陌生(使用打字稿已有2年的经验)。

As we all know, private is in fact not private in the transpiled code. 众所周知,在转译的代码中, private实际上不是 private的。 Therefore, while private member access is helpful to Typescript developers working alone or in a team, the output JavaScript is polluted with extra variables. 因此,尽管private 成员访问对单独或团队工作的Typescript开发人员有帮助,但输出JavaScript会被额外的变量污染。

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. 我们使用下划线_toLetOthersKnowOurIntent。 Angular's Style Guide suggest we avoid using _ on private variables. Angular的样式指南建议我们避免在私有变量上使用_。

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. 然后的问题是,如果您正在使用打字稿编写代码以供一个人使用JavaScript(某种包)使用,那么使用私有属性是否会更加有害且令人困惑。 Is the use or private only good for working with other TypeScript developers. 使用或private仅适合与其他TypeScript开发人员一起工作。 If you are developing a library, say an Angular component to be distributed via NPM should one just avoid private all together? 如果您正在开发一个库,比如说要通过NPM分发一个Angular组件,应该避免完全private吗?

TypeScript types and visibility features don't affect JavaScript output. TypeScript类型和可见性功能不会影响JavaScript输出。

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是Angular社区中选择的语言,因此通常会以TypeScript编写的库中尊重私有成员。

Angular style guide is opinionated and doesn't necessarily take all considerations into account. Angular样式指南是自以为是的,并不一定要考虑所有因素。 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, eg during debugging or using compiled TypeScript code with JavaScript. TypeScript可见性反映在生成的文档和IDE中,但是使用额外的方式来指定成员不公开总是有好处的,例如在调试过程中或将已编译的TypeScript代码与JavaScript一起使用。

Angular code base itself contains a lot of _ underscored members, just because this is convenient, so official style guide clearly isn't considered impeccable. Angular代码库本身包含许多_下划线的成员,仅因为这样做很方便,所以显然不认为官方样式指南是无懈可击的。 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

Another concern is that not every IDE or editor has full support for Angular templates. 另一个问题是,并不是每个IDE或编辑器都完全支持Angular模板。 Due to how AOT compilation works, it requires all members that are used in component template to be public. 由于AOT编译的工作原理,它要求组件模板中使用的所有成员都是公开的。 This won't affect JIT compilation, which is usually used for development. 这不会影响通常用于开发的JIT编译。 Making sure that private members won't be accidentally used in template becomes developer's responsibility. 确保不会在模板中意外使用私人成员成为开发人员的责任。 This is another problem that _ Hungarian notation solves. 这是_匈牙利表示法解决的另一个问题。

If there's a chance that a user can benefit from extending a class, consider using protected instead of private . 如果用户有可能从扩展类中受益,请考虑使用protected而不是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. JavaScript中没有真正的封装,因此不适当的可见性将永远不会阻止用户以用户想要的方式使用第三方代码,而只会绕圈而行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM