When you call a top-level function in Javascript, this keyword inside the function refers to the default object (window, if in a browser). I understand that this is a special case of calling a function as a method, because by default it is called in a window (as explained in John Rezig’s book "Secrets of JavaScript Ninja", page 49). Indeed, both calls in the following code are identical.
function func() {
return this;
}
console.log(func() === window);
console.log(window.func() === window);
So far so good ... Now here is the part I don’t understand:
When a function is nested in another function and called without specifying the object to be called, this keyword inside the function also refers to the window. But the internal function cannot be called in the window (see code below).
function outerFunc() {
function innerFunc() {
return this;
}
console.log(innerFunc() === window);
console.log(window.innerFunc)
}
outerFunc();
, , ... , , , ?
EDIT
.
, "" . , .
( ) , / , , .
, , "" , ( ).
bind . , "", , . , , , . , , , bind , , apply. - , , , , apply.
"", :
myFunction();
, :
, , . !