typescript 打字稿:成员的下划线约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47747809/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:06:29  来源:igfitidea点击:

Typescript : underscore convention for members

javascripttypescriptecmascript-6typescript2.0es6-class

提问by Jeson Dias

I have a class Email

我有一个班级电子邮件

class Email {
  private _from: string;
  private _to: Array<string>;
  private _subject: string;
}

It'll create an email object something like:

它将创建一个电子邮件对象,例如:

{
  _from:'',
  _to:'',
  _subject:''
}

This seems a little weird to me since I cannot directly use this object to send to a function . Instead I'll have to transform the object so that it doesn't have underscores . So how do I use the underscore convention or do I have to transform the object .

这对我来说似乎有点奇怪,因为我不能直接使用这个对象发送到一个函数。相反,我必须转换对象,使其没有下划线。那么我如何使用下划线约定或者我是否必须转换对象。

EDIT : If I do drop the '_'

编辑:如果我确实删除了“_”

How do I name the getters and setters if we name the private variables without underscore? A VSCode plugin called Typescript toolbox creates them something like this

如果我们命名不带下划线的私有变量,我该如何命名 getter 和 setter?一个名为 Typescript 工具箱的 VSCode 插件创建它们是这样的

public get $subject(): string { 
  return this.subject;
}

Is $ a good convention ?

$ 是一个很好的约定吗?

采纳答案by codejockie

Just name your private variables as you want but don't use _. You could create your own standard and stick to it.

只需根据需要命名您的私有变量,但不要使用_. 您可以创建自己的标准并坚持下去。

Setters and getters are like any other functions so you can follow the method naming convention to name.

Setter 和 getter 就像任何其他函数一样,因此您可以遵循方法命名约定来命名。

Do not use "_" as a prefix for private properties.
Use whole words in names when possible.

不要使用“_”作为私有属性的前缀。
尽可能在名称中使用整个单词。

回答by Daniel Tran

Underscore "_" prefix for private fields is out-of-date style. It's better to name your variable in a readable and friendly way.

私有字段的下划线“_”前缀是过时的样式。最好以可读和友好的方式命名变量。

See Microsoft Typescript coding convention here

请参阅此处的Microsoft Typescript 编码约定

  1. Do not use "_" as a prefix for private properties.
  1. 不要使用“_”作为私有属性的前缀。

回答by Ali Adravi

Those who say, must not use the "_", for them, here is some code from TypeScript site:

那些说一定不能使用“_”的人,对于他们来说,这是来自TypeScript 站点的一些代码:

class Employee {
    private _fullName: string;

    get fullName(): string {
        return this._fullName;
    } 
    this._fullName = ......
}

Same question on Stackoverflow, you should have a look on it, specially the answer.

关于Stackoverflow 的同样问题,你应该看看它,特别是答案。

For the time being, if accept, we should not use _, then what are other better ways?

暂时,如果accept,我们不应该使用_,那么还有什么更好的方法呢?

Let's take your example of email, if we will not use _ then, we will come something like this:

让我们以您的电子邮件为例,如果我们不使用 _ 那么,我们将是这样的:

memebr: to,      get/set: emailTo
member: from     get/set: emailFrom 

might be you can think some better name, but every time you need to think, which is not very common in developer world!

可能你可以想到一些更好的名字,但每次你都需要思考,这在开发者世界中并不常见!

Using _ and same name for property is easy to check the code otherwise we will be keep mapping which property to which member.

使用 _ 和相同名称的属性很容易检查代码,否则我们将保持将哪个属性映射到哪个成员。

BUT:If you have been forced by your lead on company then your can use $for member and property without it, not a rule but easy way:

但是:如果您被公司的领导强迫,那么您可以$在没有它的情况下将其用于会员和财产,这不是规则而是简单的方法:

class Employee {
    private fullName$: string;

    get fullName(): string {
        return this.fullName$;
    } 
    this.fullName$ = ......
}

The Choice Is Yours!!!

这是你的选择!!!