Hi I am new in JavaScript. I am wondering if the class constructor is a special class static method. I have such an example:
class warrior {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
sayHello() {
return `Hello my name is ${this.fullName}`;
}
static createWarrior(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
return this;
}
}
let warrior1 = new warrior("Ashley", "Yuna", 25);
warrior1.sayHello();
let warrior2 = warrior.createWarrior("Kris", "Snow", 23);
warrior2.sayHello();
If class constructor is a kind of static method, how can I modify createWarrior
static method to be able to return warrior instance?