Your function B
is just declared in the function A
, it has nothing else with function A
, except the scope. Every function has it own context to which this
refers. By default (means no explicit/implicit bindings, no object) this
refers to the window
object in non strict
mode and to the undefined
in strict
mode.
Non strict mode
function A() {
return this;
}
console.log(A());
Strict mode
'use strict';
function A() {
return this;
}
console.log(A());
Explicit binding
'use strict';
const obj = { name: 'Object' };
function A() {
return this;
}
console.log(A.call(obj));
Object binding
const obj = {
name: 'Object',
A() {
return this;
}
}
console.log(obj.A());