i have the following object. i´m trying to reach the variable b from inside the callback of setTimeout but it doesn’t work. i know that the callback of setTimeout only knows the variables of the its surrounding function, so how can i reach 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();
1 Answer