TypeScript 中类属性的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40587873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention for class properties in TypeScript
提问by lenny
According to the offical style guideyou should
根据官方风格指南,你应该
Avoid prefixing private properties and methods with an underscore.
避免使用下划线作为私有属性和方法的前缀。
As I come from a Java background, I usually would just use the this
keyword:
由于我来自 Java 背景,我通常只会使用this
关键字:
export default class Device {
private id: string;
constructor(id: string) {
this.id = id;
}
public get id(): string { // [ts] Duplicate identifier 'id'.
return this.id;
}
public set id(value: string) { // [ts] Duplicate identifier 'id'.
this.id = value;
}
}
But the TypeScript compiler complains: [ts] Duplicate identifier 'id'.
但是 TypeScript 编译器抱怨:[ts] 重复标识符“id”。
Is there a convention or best practice for parameter naming in a TypeScript constructor?
TypeScript 构造函数中的参数命名是否有约定或最佳实践?
EDIT
Sorry I missed the essential part of the code which actually causes the TS compiler error.
Using the getand setproperty of TypeScript produces the error.
编辑
抱歉,我错过了实际导致 TS 编译器错误的代码的重要部分。
使用TypeScript的get和set属性会产生错误。
So my updated question: Is there a way to follow the style guide and also use the get/set properties of TypeScript?
所以我更新的问题是:有没有办法遵循样式指南并使用 TypeScript 的 get/set 属性?
回答by Erik Cupal
Answer
回答
If you want to use get
and set
accessors, you have to prefix the private property with underscore. In all other cases don't use it. I would say using underscore with accesors is a special case and although it's not explicitely written in Coding guidelines, it doesn't mean it's wrong. They use it in the official documentation.
如果要使用get
和set
访问器,则必须在私有属性前加上下划线。在所有其他情况下不要使用它。我会说将下划线与 accesors 一起使用是一种特殊情况,虽然它没有明确地写在Coding Guidelines 中,但这并不意味着它是错误的。他们在官方文档中使用它。
Reason for the underscore
下划线的原因
For start, I would like to emphasize the difference between field
and property
. In standard high level OOP languages like Java or C#, field is a private member which shouldn't be visible to other classes. If you want to expose it with encapsulation in mind, you should create a property.
对于开始,我想强调的区别field
和property
。在 Java 或 C# 等标准高级 OOP 语言中,field 是一个私有成员,其他类不应该看到它。如果您想在考虑封装的情况下公开它,您应该创建一个属性。
In Javayou do it this way (it is called Bean properties):
在Java 中,你这样做(它被称为Bean 属性):
private int id;
public int getId() {
return this.id;
}
public setId(int value) {
this.id = value;
}
Then you can access the property by calling these methods:
然后您可以通过调用这些方法访问该属性:
int i = device.getId();
device.setId(i);
//increment id by 1
device.setId(device.getId() + 1);
On the other hand, C#was designed so that it's much easier to use properties:
另一方面,C#被设计为更容易使用属性:
private int id;
public int Id {
get {
return this.id;
}
set {
this.id = value;
}
}
(value is always the assigned value.)
(值始终是指定的值。)
You can directly assign values to these properties or get the property values.
您可以直接为这些属性赋值或获取属性值。
int i = device.Id;
device.Id = i;
//increment id by 1
i
In plain JavaScript, there are no real fields, because the class members are always public; we simply call them properties.
在普通的 JavaScript 中,没有真正的字段,因为类成员总是公开的;我们简单地称它们为属性。
In TypeScript, you can define "true" C#-like properties (with encapsulation). You use Accessorsfor that.
在TypeScript 中,您可以定义“真正的”类似C# 的属性(带封装)。您可以使用访问器为。
private _id: number;
public get id(): number {
return this._id;
}
public set id(value: number) {
this._id = value;
}
Usage:
用法:
let i: number = device.id;
device.id = i;
//increment id by 1
device.id++;
You haveto use underscore here because of two reasons:
由于两个原因,您必须在此处使用下划线:
- In JavaScript, all class members are public. Therefore, by putting an underscore before private property, we sign, that this property (field) is private and should be accessed by it's public property only.
- If you named both the private and the public property with same name, the JavaScript interpreter wouldn't know whether to access the private or public property. Thus you get the error you're writing about: [ts] Duplicate identifier 'id'.
- 在 JavaScript 中,所有类成员都是公共的。因此,通过在私有属性前加上下划线,我们表示该属性(字段)是私有的,只能由它的公共属性访问。
- 如果您将私有和公共属性都命名为相同的名称,JavaScript 解释器将不知道是访问私有属性还是公共属性。因此你会得到你正在写的错误:[ts] Duplicate identifier 'id'。
回答by Zartc
If the question is strictly :
如果问题是严格的:
Is there a way to follow the [typeScript]style guide and also use the get/set properties of TypeScript?
有没有办法遵循[typeScript]样式指南并使用 TypeScript 的 get/set 属性?
Where the TypeScript Style Guide says :
TypeScript 风格指南说:
Avoid prefixing private properties and methods with an underscore.
避免使用下划线作为私有属性和方法的前缀。
Then you can use the $
(dollar sign) instead of the _
(underscore) to prefix your private fields. In this way you both get rid of the [ts] Duplicate identifier 'blablabla'
error while still respecting the TypeScript Style Guide.
然后您可以使用$
(美元符号)而不是_
(下划线)作为您的私有字段的前缀。通过这种方式,您既可以消除[ts] Duplicate identifier 'blablabla'
错误,同时仍然遵守 TypeScript 样式指南。
In addition, but it is just my opinion, the .$
combination is more readable than the ._
combination.
另外,但只是我的意见,.$
组合比._
组合更具可读性。
回答by Vitaliy Markitanov
For properties accessors you use _
.
对于属性访问器,您使用_
.
See sample from Microsoft https://www.typescriptlang.org/docs/handbook/classes.html#accessors:
请参阅 Microsoft https://www.typescriptlang.org/docs/handbook/classes.html#accessors 的示例:
let passcode = "secret passcode";
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
}
let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
console.log(employee.fullName);
}