This is because
this.name
is in the context of the function
f1
, but not in the function you returned from
f1
, which has different "this", the object which does not contain
name
property.
For example, compare:
o = {
name: "the o_object",
oo: {
name: "zhuzhu",
f1: function() {
return this.name;
}
}
};
alert(o.oo.f1());
[EDIT #1]
Now, let's figure out what is that "different this" used in return statement in
f1
. This is the function object
f1
itself. To check it up, let's give it a property to identify it, and call this property
innerName
. Then out check-up will show the value of this property:
o = {
name:"the o_object",
oo: {
name: "zhuzhu",
f1: function(){
innerName = "function itself";
return function () {
return this.innerName;
}
}
}
};
alert(o.oo.f1()());
[EDIT #2]
By the way, the second case presents the interesting example of
closure, a very important concept in programming. Please see:
http://en.wikipedia.org/wiki/Closure[
^].
—SA