I'm encountering with a little problem with the context of this
:
In JavaScript this
always refers to the "owner" of the function we're executing, or rather, to the object that a function is a method of.
So, this code :
var o={
f:function ()
{
console.log(this); //the owner of the function is `o`
}
}
console.log(o.f()) // this shows the `o` as this
all OK.
So why this code
var o = {
f: function ()
{
return function ()
{
console.log(this);
}
}
}
console.log(o.f()())
Shows that this
is the global object/window ?
o.f()
returns a function , and then I execute it.but still the Hoster object is o
. so why it shows the hoster as window
?