Why does this
in x.prop2
returns Undefined
but it works when trying to use it in x.prop3
through a method?
I have been told it's because of difference of scope between prop2
and prop3
, but can anyone explain to me in detail the reason for that behavior?
let x = {
prop1: 10,
prop2: this.prop1,
prop3: function () {
return this.prop1
}
};
console.log(x.prop2) // undefined
console.log(x.prop3()) // 10