The value this
references depends on circumstances. Any unscoped (i.e. not in an object) function call will have this
= window
(DOMWindow
, if you prefer). Anything that is part of a prototype, or has been changed using apply
or bind
, will have this
as that object ("itself", if you prefer).
So, to illustrate. When you are instantiating using the new
keyword, your function automatically inherits this
as itself. When you call an IETF within it, this
within this IETF will point to the global scope.
If you would like to avoid this, instead of calling testMe
literally, you can always do this:
var myFunction = function() {
var testMe = function() {
console.log(this);
}
testMe.bind(this);
}
Which will, in turn, have this
within testMe
use the object as it should.