I am facing an issue with accessing the variable b
from inside the callback of setTimeout
. I understand that the callback function of setTimeout only has access to the variables within its surrounding function. How can I access the this.b
? Thank you!
function someFunc() {
this.a = 10;
this.b = 20;
this.func = function() {
this.c = 50;
console.log("a = " + this.a); //works
var time = setTimeout(function() {
console.log("b = " + someFunc.b); //this.b doesn't work
console.log("C = " + this.c); //why this doesn't work also? says undefined
},1000);
}
}
var m = new someFunc();
m.func();