This question led to an interesting discussion in the comments, focusing on the advantages of Static Factory Methods versus Constructors.
Consider the following example:
export class User extends Model<UserProps> {
// Recommended Method (Without a constructor)
static buildUser(attrs: UserProps) : User {
return new User(new Attributes(attrs), new Eventing(), new ApiSync(rootUrl));
}
// Alternatively, why not do it this way? No need to call User.buildUser()
constructor(attrs: UserProps) {
super(new Attributes(attrs), new Eventing(), new ApiSync(rootUrl));
}
}
I realize that by only implementing the buildUser method, the constructor with super and arguments will be implicitly called if the constructor is not defined explicitly.
However, are there any features that buildUser provides which a direct super call within the constructor does not?
It just seems like extra code to create a user.
let user = User.buildUser(some attr)
versus
let user = new User(some attr);