TypeScript 中类属性的命名约定 [英] Naming convention for class properties in TypeScript

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

问题描述

根据官方风格指南你应该

避免使用下划线作为私有属性和方法的前缀.

Avoid prefixing private properties and methods with an underscore.

因为我有 Java 背景,所以我通常只会使用 this 关键字:

As I come from a Java background, I usually would just use the this keyword:

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;
    }
}

但 TypeScript 编译器抱怨:[ts] 重复标识符id".

But the TypeScript compiler complains: [ts] Duplicate identifier 'id'.

是否有 TypeScript 构造函数中参数命名的约定或最佳实践?

Is there a convention or best practice for parameter naming in a TypeScript constructor?

使用 TypeScript 的 getset 属性会产生错误.

Using the get and set property of TypeScript produces the error.

有没有办法遵循样式指南并使用 TypeScript 的 get/set 属性?

Is there a way to follow the style guide and also use the get/set properties of TypeScript?

推荐答案

答案

如果你想使用 getset 访问器,你必须用下划线作为私有属性的前缀.在所有其他情况下不要使用它.我会说将下划线与访问器一起使用是一种特殊情况,尽管它没有明确写在 编码指南,这并不意味着它是错的.他们在官方文档中使用它.

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 accessors is a special case and although it's not explicitly written in Coding guidelines, it doesn't mean it's wrong. They use it in the official documentation.

首先,我想强调fieldproperty 之间的区别.在 Java 或 C# 等标准高级 OOP 语言中,field 是一个私有成员,其他类不应该看到它.如果你想用封装来公开它,你应该创建一个属性.

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.

Java 中,您可以这样做(称为 Bean 属性):

In Java you do it this way (it is called Bean properties):

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);

另一方面,C# 被设计为更易于使用属性:

On the other hand, C# was designed so that it's much easier to use properties:

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
device.Id++;


在普通的 JavaScript 中,没有真正的字段,因为类成员总是公开的;我们简单地称它们为属性.


In plain JavaScript, there are no real fields, because the class members are always public; we simply call them properties.

TypeScript 中,您可以定义true";类似 C# 的属性(带封装).为此,您可以使用 Accessors.

In TypeScript, you can define "true" C#-like properties (with encapsulation). You use Accessors for that.

private _id: number;

public get id(): number {
    return this._id;
}

public set id(value: number) {
    this._id = value;
}

用法:

let i: number = device.id;
device.id = i;

//increment id by 1
device.id++;

必须在此处使用下划线,原因有两个:

You have to use underscore here because of two reasons:

  1. 在 JavaScript 中,所有类成员都是公共的.因此,通过在私有属性前加上下划线,我们表示此属性(字段)是私有的,只能由其公共属性访问.
  2. 如果您将私有属性和公共属性都命名为相同的名称,JavaScript 解释器将不知道是访问私有属性还是公共属性.因此你会得到你正在写的错误:[ts] Duplicate identifier 'id'.

这篇关于TypeScript 中类属性的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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