I have this simple code :
var o = {
a: 1,
b: 2,
f1: function ()
{
alert(this.b);
}
}
var o2 = {
a: 11,
b: 22,
f2: function (j)
{
j();
}
}
But running this code :
o2.f2(o.f1)
yields undefined. ( while im expecting "22" as a result)
Now, I know that the context has gone somewhere. and hence If I change the code in o2
to :
f2: function (j)
{
j.apply(this);
}
It does work obviously.
But my question is :
- In what stage did i lose the context ?
I don't understand : when j()
is running , there is a b
property in the o2
object.
What am I missing ?