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

查看:296
本文介绍了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?

编辑
抱歉,我错过了实际上导致TS编译器错误的代码主要部分.
使用TypeScript的 get set 属性会产生错误.

EDIT
Sorry I missed the essential part of the code which actually causes the TS compiler error.
Using the get and set property of TypeScript produces the error.

所以我更新的问题是:有没有一种方法可以遵循样式指南并使用TypeScript的get/set属性?

推荐答案

答案

如果要使用getset访问器,则必须在私有属性前添加下划线.在其他所有情况下,请勿使用.我想说带下划线和accesors是一个特例,尽管它没有明确地写在编码准则,这并不意味着它是错误的.他们在官方文档中使用它.

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.

首先,我想强调fieldproperty之间的区别.在标准的高级OOP语言(如Java或C#)中,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
i


在普通的JavaScript中,没有实际字段,因为类成员始终是公共的;我们简单地称它们为属性.


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

TypeScript 中,您可以定义真实的"类似于C#的属性(带有封装).为此,您使用访问器.

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]重复的标识符'id'.

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

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