I am trying to access the name defined in the constructor via a method, but it is returning undefined. Here is the simple code:
class Person {
constructor(){
let name = 'Tom';
}
logName(){
console.log(this.name);
}
}
let x = new Person();
x.logName();
this.namein order to assign it as a property of the instantiated object - else it's just a free variable, with all the usual scoping ruleslet this.name = 'Tom'returns error in constructor. Why?this.name = "Tom";.this.nameis not an identifier.varorletto define properties