I am trying to learn about constructors in JavaScript. I was watching some tutorial where this constructor:
class Human{
constructor() {
this.gender = 'male'
}
printGender(){
console.log(this.gender);
}
}
was also written with shorter syntax that looked like this:
class Human{
gender = 'male';
printGender = () =>{
console.log(this.gender);
}
}
I have no problem understanding this. However, what if I have some parameters. Like this for example:
class Human{
constructor(gender, height) {
this.gender = gender;
this.height = height;
}
printGender(){
console.log(this.gender);
}
}
How do I write this shorter syntax and also have parameters? I could not find anything about this question. Any help?
The code you wrote would be acceptable.
You could additionally do:
class Human {
gender;
height;
constructor(gender, height) {
this.gender = gender;
this.height = height;
}
printGender(){
console.log(this.gender);
}
}