简体   繁体   English

TypeScript getter setter 约定

[英]TypeScript getter setter convention

What is the convention (standard) in TypeScript for class attributes? TypeScript 中类属性的约定(标准)是什么?

In the angular 2 demo (The Heroes Tour from angular.io) all attributes are set to public :在 angular 2 演示(来自 angular.io 的 The Heroes Tour)中,所有属性都设置为 public :

export class Hero {
   id: number;
   name: string;
}

So they can be instanciated both ways :所以它们可以通过两种方式实例化:

var hero: Hero = new Hero();
hero.id = 0;
hero.name = "hero";

or或者

var hero2: Hero = {id : 0, name: "hero"};

Is there a Java style convention (like this) :是否有 Java 风格约定(像这样):

export class Hero {
   private id: number;
   private name: string;

   setId(id: number): Hero {
      this.id = id;
      return this;
   }

   setName(name: string): Hero {
      this.name = name;
      return this;
   }

   getId(): number {
      return this.id;
   }

   getName(): string {
      return this.name;
   }
}

Declaration (exemple) :声明(示例):

var hero: Hero = new Hero();
hero.setId(0).setName('hero');

var hero2: Hero = new Hero().setId(0).setName('hero');

Is there a Java style convention (like this)是否有 Java 风格约定(像这样)

you can do that (and in fact I have).你可以做到这一点(事实上我做到了)。 People use getter / setter as well:人们也使用getter / setter

export class Hero {
   private _id: number;

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

However, be careful not to put too much logic in setters: https://basarat.gitbooks.io/typescript/content/docs/tips/propertySetters.html .但是,请注意不要在 setter 中放置太多逻辑: https : //basarat.gitbooks.io/typescript/content/docs/tips/propertySetters.html I generally prefer explicit function calls.我通常更喜欢显式函数调用。

var hero2: Hero = {id : 0, name: "hero"};

This is a weakness (or convenience strength) of the structural nature of TypeScript's type checking.这是 TypeScript 类型检查的结构性质的一个弱点(或方便强度)。 More on this. 更多关于这一点。

The typescript is converted into plan javascript, so... if you need something before or after set value, how to log, you to implement.打字稿被转换成计划javascript,所以......如果你需要设置值之前或之后的东西,如何记录,你来实现。 If no needed, getter and setters is overkill.如果不需要,getter 和 setter 是多余的。 The instantiate and setters, for me, is the same to Java.对我来说,实例化和设置器与 Java 相同。

Example:例子:

export class Person{
    public name: string;
    public anotherAttribute: string;
    private age: number;
    private _birth: Date;

    public set birth(birt: Date){
       this._birth = birth;
       this.age = calculateAge(birth);
    }
    .......
}


let person: Person = new Person();
person.name = "John";
......

is there a java style convention是否有 Java 风格约定

Well you can do it if you want but since they're different languages you probably should use typescript conventions which usually directly access "members" via the .好吧,如果您愿意,您可以这样做,但由于它们是不同的语言,您可能应该使用打字稿约定,这些约定通常通过 . notation.. that is way more readable to anyone who works with typescript.. and arguably to java devs as well :) as much as I love java I do realise coding conventionally would have me typing 2 times what is useful.符号..对于使用打字稿的任何人来说,这都更具可读性..并且可以说对于Java开发人员来说也是如此:)尽管我很喜欢Java,但我确实意识到传统的编码会让我输入2倍有用的东西。

In fact it's a common joke among developers that languages such as java are overdeclarative事实上,Java 之类的语言过度声明是开发人员之间的一个常见笑话

tl;dr no.. use myObject.myMember to access members directly tl;dr no.. 使用 myObject.myMember 直接访问成员

Is it because of the extra code around setting the individual properties that you want to use the Java style?是因为围绕设置您想要使用 Java 样式的单个属性的额外代码吗? If so you can declare the properties in the constructor of the class.如果是这样,您可以在类的构造函数中声明属性。

hero class英雄类

export class Hero {
 constructor(public id?: number, public name?: string) {}
}

declarations声明

const hero1 = new Hero(1, 'hero');
const hero2 = new Hero();

Java is a statically compiled language. Java 是一种静态编译语言。 In Java, if you publish a .jar library with a class在 Java 中,如果您发布一个带有类的 .jar 库

class Foo {
  public int bar;
}

and later decide to introduce a logic around that field然后决定围绕该领域引入逻辑

class Foo {
  private int _bar;
  public int getBar() { return _bar - 1; }
  public void setBar(int v) { _bar = v + 1; }
}

any code that uses your .jar will break and will have to be updated and re-compiled.任何使用您的 .jar 的代码都会中断,必须更新和重新编译。 This is why it's a big no-no in Java to expose raw public fields.这就是为什么在 Java 中公开原始公共字段是一个很大的禁忌。

TypeScript is a superset of JavaScript which is a dynamic language. TypeScript 是 JavaScript 的超集,它是一种动态语言。 All linking is dynamic.所有链接都是动态的。 You can safely release a library with a class您可以安全地发布带有类的库

class Foo {
  bar: number
}

If you later release an update如果您稍后发布更新

class Foo {
  private _bar: number
  get bar() { return this._bar - 1 }
  set bar(v: number) { this._bar = v + 1 }
}

your library users won't notice.您的图书馆用户不会注意到。

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

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