Here's an example
function banana() {
this.size = 5;
this.say_size = function() {
console.log(this.size);
}.bind(this);
}
banana.prototype.say_size2 = function() {
console.log(this.size);
}
var b = new banana();
b.say_size();
b.say_size2(); //TLDR: why does this line work without binding?
setTimeout(b.say_size, 100);
setTimeout(b.say_size2, 200);
So, in the case I call b.say_size or b.say_size2 directly, 'this' refers to the banana object and I get '5' as output on both.
In case the function is called by a timer or an event though, 'this' is set to window unless the called function is bound to the object.
What I don't understand is how say_size2 gets the right 'this' when called directly without the prototype function being bound to anything?