I know that this keyword always refers to the this of the current scope, which changes any time you wrap something in function() { ... }. My question is, why do I have access to the outer scope variable x in the function inside the setTimeout function?
var x = 45;
function getRecipe(recipe) {
x = 35;
return {
displayRecipe: function(a) {
//here we have access to x=35
setTimeout(function() {
//why do we have access to x=35 and to recipe here?
console.log(this.x + a + recipe);
}, 1500)
}
}
}
getRecipe("noodles").displayRecipe(2);