How can I get "back to previous this? To explain better, I created this sample code:
class MyClass {
constructor(x,y,z) {
this.x = x;
this.y = y;
setTimeout(function () {
this.z = z;
}, 1000);
}
}
var myVariable = new MyClass(10,15,20);
setTimeout(function () {
console.log(myVariable.z);
}, 1500);
The problem is that last lane should output 20 but instead it gives me an error.
I understand why though. The this.z was understood as this of setTimeout(), not myClass, making myVariable.z as undefined.
How can I set this of myVariable inside my MyClass?