To add to other answers:
When a function is called, this set depending on how it is called. If it is called using myfunc.call or myfunc.apply , then this set to the first argument passed. If it is called in "dotted" form, that is, myObject.myfunc() , then this set to everything that is before the point.
There is an exception to this rule that allows you to bake this in with bind , in which case it will be all that was connected. that is, in var boundfunc = myfunc.bind(myobj); then boundfunc is called any time, it will be like calling myfunc , but with this , referring to myobj , regardless of anything else. Here is an example that does this:
function F() { var C = function() { return this; }.bind(this); return C(); } var o = new F();
If none of these cases apply, then this always either a global object ( window if you are in a browser), or if you are in strict mode, it is undefined .
source share