In a lot of use cases, the fields are initialized in the constructor.
class Person {
// Fields declaration
name : string;
private id : string;
// Initial the field value in constructor
constructor(name : string, id : string) {
this.name = name;
this.id = id;
}
}
You can simplify to
class Person {
constructor(private name : string, public id : string) {}
}
No comments:
Post a Comment