I am currently following Angular's official documentation called The Tour of Heroes
and I have made slight modifications to it. As a result, there are now two distinct ways to define a class as shown below:
user.ts
export class User {
url: string;
id: number;
username: string;
email?: string;
images: string[];
files?: number[];
password?: string;
first_name?: string;
last_name?: string;
}
The first method is the conventional way to define a class for validating a User
's attributes. Additionally, I am able to create a new instance of User
like this:
newUser: User = {url: '', id: 0, username: 'Alpha', images: []};
Recently, I came across a new approach to defining a class:
images.ts
export class Image {
constructor(
public id: number,
public created: string,
public userId: number,
public fileUrl: string,
public owner: string,
public des?: string,
) { }
}
I can now create a new instance of Image
like this:
newImage = new Image(50, '20170822', 12, '', 'Belter');
I am curious to know the differences between these two methods and which one would be more preferable to use.