简体   繁体   中英

TypeScript getter setter convention

What is the convention (standard) in TypeScript for class attributes?

In the angular 2 demo (The Heroes Tour from angular.io) all attributes are set to 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) :

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)

you can do that (and in fact I have). People use getter / setter as well:

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 . 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. 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. If no needed, getter and setters is overkill. The instantiate and setters, for me, is the same to 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

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.

In fact it's a common joke among developers that languages such as java are overdeclarative

tl;dr no.. use myObject.myMember to access members directly

Is it because of the extra code around setting the individual properties that you want to use the Java style? 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. In Java, if you publish a .jar library with a class

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. This is why it's a big no-no in Java to expose raw public fields.

TypeScript is a superset of JavaScript which is a dynamic language. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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