I am learning es6 js and found a way to declare class and use its method.
but one thing is confusing which i want to know.
class myGender {
var gender = "male";
printMyGender(){
console.log(gender);
}
}
const gender = new myGender();
gender.printMyGender();
In the above code i am getting error why i can't access the gender variable in the same class and other scenario
class myGender {
constructor(){
this.gender = "male";
}
printMyGender(){
console.log(this.gender);
}
}
const gender = new myGender();
gender.printMyGender();
this works as charm !.