Answer the question
In order to leave comments, you need to log in
How to correctly understand such a function call?
var a = 1;
function getFunc() {
var a = 2;
var func = function() { alert(a); };
return func;
}
getFunc()();
Answer the question
In order to leave comments, you need to log in
This is the same as
var a = 1;
function getFunc() {
var a = 2;
var func = function() { alert(a); };
return func();
}
getFunc();
This getFunc() function returns a function that remembers its environment, i.e. local variable "a".
And then comes the call to the function that was returned. getFunc() () ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question