I've just read the book "Scope and Closures" of the series You Don't Know Javascript. The book has an appendix talking about Lexical this. It gives an example of how this can lose its binding and how can be solved using arrow functions.
I'm having problems with the example of the first situation. This is the example of the book:
var obj = { id: 2, cool: function(){console.log(this.id)}}
id=3;
obj.cool();
setTimeout(obj.cool, 100);
This works fine an prints the expected result: 2 the first time, and 3 in the setTimeout call.
But if I change this call by:
var obj = { id: 2, cool: function(){console.log(this.id)}}
id=3;
obj.cool();
setTimeout(function(){obj.cool()}, 100);
It prints 2 both times.
My guess is that, by enclosing the call to obj.cool inside a function, I'm creating a scope where id has the expected value.
Am I right? Is this the right explanation? Am I missing something?