D
D
Daniil Igorevich2016-01-23 20:25:11
JavaScript
Daniil Igorevich, 2016-01-23 20:25:11

How to correctly understand such a function call?

var a = 1;
function getFunc() {
  var a = 2;
  var func = function() { alert(a); };
  return func;
}
getFunc()();

Question about getFunc()();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AlexRas, 2016-01-23
@Petr_Anisimov

This is the same as

var a = 1;
function getFunc() {
  var a = 2;
  var func = function() { alert(a); };
  return func();
}
getFunc();

P
Peter, 2016-01-23
@petermzg

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() () ;

M
maaGames, 2016-01-23
@maaGames

Calling a functor by a returned function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question