以简单/缩小的方式定义类成员

时间:2016-07-28 16:53:54

标签: typescript angular

我的Angular 2项目中有大约15个字段的大类定义。

class Employee {
    private id: number;
    private name: string;
    private department: string;
    private title?: string;
    // 10 more fields 

    constructor(_id: number,
                _name: string,
                _department: string,
                _title: string
                // 10 more input parameters
                ) {
        this.id = _id;
        this.name = _name;
        this.department = _department;
        this.title = _title;
        // 10 more assignments
    }
}

这看起来和工作正常,但是对于所有15个字段来说它变得非常长:每个班级成员 3行

有没有办法简化/缩小定义,仍然可以使用new创建对象?像这样:

let person1 = new Employee(1, 'Andrei', 'Dev', 'Developer')

更新:我找到了解决方案。

class Employee {
    constructor(private id: number,
                private name: string,
                private department: string,
                private title: string
                // 10 more input parameters
                ) {
    }
}

使用访问说明符(constructorpublic)声明private的输入时,它会做两件事:

  1. 创建成员变量
  2. 指定输入值
  3. 我从未在官方的打字稿文档中看到它,这很奇怪。

    我在 Angular 2 docs https://angular.io/docs/ts/latest/guide/forms.html中找到了它。但是,我怀疑它与Angular2特别相关。

    Update2 :更完整和最新的 Typescript 文档https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.3.1(感谢torazaburo)

4 个答案:

答案 0 :(得分:2)

在这种情况下,我不确定构建器模式是否会为您完成,但同样,它是个人品味:

class Employee {
    private id: number;
    private name: string;
    private department: string;
    private title: string;
}

class EmployeeBuilder {
    private _id: number;
    private _name: string;
    private _department: string;
    private _title: string;

    id(value: number): EmployeeBuilder {
        this._id = value;
        return this;
    }

    name(value: string): EmployeeBuilder {
        this._name = value;
        return this;
    }

    department(value: string): EmployeeBuilder {
        this._department = value;
        return this;
    }

    title(value: string): EmployeeBuilder {
        this._title = value;
        return this;
    }

    build(): Employee {
        let employee: any = new Employee();

        employee.id = this._id;
        employee.name = this._name;
        employee.department = this._department;
        employee.title = this._title;

        return employee;
    }
}

然后你可以像这样构建一个实例:

let employee = new EmployeeBuilder()
                    .id(100)
                    .name("name")
                    .department("department")
                    .title("title")
                    .build();

code in playground

答案 1 :(得分:2)

你可以这样做。使用?关键字

进行创建时,new会将其设为可选项
export class Dragon {
    constructor(
        public id: number,
        public name: string,
        public details?: string,
        public skill?: string,
        public imageUrl?: string,
        public abilites?: string[]
    ) { }
}

现在你可以制作一个新的对象,而不是给出所有的字段

  

dragonObject: Dragon = new Dragon(1, 'Drogon');

如果您将public id: number替换为public id?: numberpublic name: string替换为public name?: string

现在,对象创建将像dragonObject: Dragon = new Dragon();

一样简单

答案 2 :(得分:2)

不是传递十个参数,而是传入一个对象,然后可以使用Object.assign en masse 的属性分配到实例上:

class Employee {
    private id: number;
    private name: string;
    private department: string;
    private title: string;
    // 10 more fields 

    constructor(obj) {
      Object.assign(this, obj);
    }
}

顺便说一下,将问号定义为private title?: string的属性是无效的语法。这是接口或参数,而不是成员。

对于类型安全,请为类似员工的对象定义接口:

interface IEmployee {
  id: number;
  name: string;
  department: string;
  title?: string;
  ...
}

然后将课程写为

class Employee implements IEmployee {

  constructor(obj: IEmployee) {
    Object.assign(this, obj);
  }

现在创建一个新员工

new Employee({
  id: 22,
  name: 'Bob',
  department: 'HR'
});

并且您的对象将针对IEmployee进行类型检查,以获取有效/缺失的属性。

这也有一个优点,就是您可以轻松克隆员工:

new Employee(otherEmployee)

答案 3 :(得分:0)

解决方案是在构造函数的变量前面添加一个访问权限指定(privatepublicprotected):

class Employee {
    constructor(private id: number,
                private name: string,
                private department: string,
                private title: string
                // 10 more input parameters
                ) {
    }
}

它做了两件事:

  1. 创建成员变量
  2. 指定输入值
  3. 我从未在官方的打字稿文档中看到它,这很奇怪。

    我在 Angular 2 docs https://angular.io/docs/ts/latest/guide/forms.html中找到了它。但是,我怀疑它与Angular2特别相关。

    更完整和最新的 Typescript 文档https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.3.1(感谢torazaburo)