TypeScript中的类属性约定(标准)是什么?TypeScript getter setter约定
在角2演示(英雄游angular.io)的所有属性都设置为公开:
export class Hero {
id: number;
name: string;
}
这样他们就可以被实例化两种方式:
var hero: Hero = new Hero();
hero.id = 0;
hero.name = "hero";
或
var hero2: Hero = {id : 0, name: "hero"};
是否有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;
}
}
宣言(为例):
var hero: Hero = new Hero();
hero.setId(0).setName('hero');
var hero2: Hero = new Hero().setId(0).setName('hero');
可能重复的http://stackoverflow.com/questions/12827266/get-and-set-in-typescript –
这可能有帮助吗?http://stackoverflow.com/questions/12713659/typescript-private-members –
注意'英雄instanceof Hero === true'而'hero2英雄=== false'因此它们是不一样的。 –