The variable is pointing to an instance of an object created via function contructor. However, when I try to call its methods inside of a ternary operator, interpreter says that the variable isn't defined. (Please, omit the fact that methods will lose context, I know that)
I've noticed, that declaring a variable via "var" statement, solves the problem, but I don't understand why.
class Person {
constructor(name) {
this.name = name
}
say() {
console.log('Hello, my name is ' + this.name)
}
justName() {
console.log(this.name)
}
}
let pers = new Person('Max')
(false ? pers.say : pers.justName)() // ReferenceError: pers is not defined