EDIT: I think this isn't a duplicate; it being a hidden parameter, I wanted to ask about shadowing in relation to this
, and lexical scoping, having read this SO Q/A re. shadowing.
I had thought that the meaning of the name this
might be resolved kind of dynamically (in terms of scoping), so as to explain why it doesn't seem to me to be resolved lexically:
function foo() {return this;}
var a = {f: foo};
var b = {f: foo};
a.f() !== b.f();
a.f() !== foo();
a.f() !== window; //not strict mode
But then I read that every function receives this
as an additional parameter, silently. (I realise arrow functions are different.)
Obviously helper()
doesn't work as we might hope:
ob = {
meth: function(){
var helper = function() {return this;};
return helper();
}
};
ob.meth(); //Window or undefined
As far as I understand, rather than this
being resolved by looking at the enclosing scope (the result being ob
), instead the interpreter is calling helper()
with this
set to undefined
(strict mode), silently passed as an argument.
So is the surrounding scope's this
effectively being shadowed, hence lexical scoping is in fact in action?