var obj = {
id: 1,
getId: function(){
console.log(this.id)
}
}
obj.getId(); // 1
setTimeout(obj.getId, 1000); // undefined
So I am trying to understand that calling the method directly is doing well but when I call it using the setTimeout method, the scope somehow disappears. Why is that? Is that a flaw in the language or is there something else going on behind the scenes that I don't understand entirely.
I even tried var self = this;
in the getId
method but it still would say self.id
is undefined. What is going on?
PS: I am using node v6.5.0 to run this code through.