Javascript Angular/Typescript接口和实现类:包含“@,$”字符的属性
我在Angular应用程序中遵循JHipster的方法,我相信这是基于最佳实践的。就模型而言,他们就是这样做的:Javascript Angular/Typescript接口和实现类:包含“@,$”字符的属性,javascript,angular,typescript,typescript-class,Javascript,Angular,Typescript,Typescript Class,我在Angular应用程序中遵循JHipster的方法,我相信这是基于最佳实践的。就模型而言,他们就是这样做的: export interface IPerson { id: number; firstName: string; lastName: string; } export class Person implements IPerson { constructor( public id: number, public firstName: string
export interface IPerson {
id: number;
firstName: string;
lastName: string;
}
export class Person implements IPerson {
constructor(
public id: number,
public firstName: string,
public lastName: string) {}
}
我在尝试添加以下属性时遇到了问题(由我正在使用的第三方API指定,还有许多其他属性包含其他特殊字符,例如$
等):
因此,问题似乎不是类本身的用法,而是构造函数中的用法,以这种方式声明
有人知道如何至少解决这个问题吗?我不知道为什么这是“最佳实践”,但如果您想分配构造函数中的所有属性,那么您只需为构造函数参数选择另一个名称
class Person implements IPerson {
'@type': string;
constructor(type: string, public name: string) {
this['@type'] = type;
}
}
然而,如果Person
没有附加任何行为,我就看不到创建类有多大价值。我会的
const person: IPerson = { '@type': 'some_type', name: 'developer10' };
我甚至会说,具有长参数列表的构造函数是“糟糕的做法”
const person = new Person('this', 'that', 'first', 'last); // is that the correct order i dunno..
为什么在构造函数中使用
public'@type':string
。还有其他方法吗?您也可以在构造函数外部将其初始化为类变量。
const person = new Person('this', 'that', 'first', 'last); // is that the correct order i dunno..