I have a basic fundamental question. I execute the below code.
var app = {
x: 10,
start: function() {
window.setTimeout(this.callback.bind(this), 10);
},
callback: function() {
function print() {
console.log('p '+ this.x);
}
console.log('c '+ this.x);
print();
}
};
app.start();
You can execute this code on jsbin
I expect it to output c 10
and p 10
. Instead it outputs c 10
and p undefined
. On further inspection, seems like this
inside print()
points to window
object. Why would this be happening?