Can someone explain to me how this function works?

I am learning code, and I am trying to understand higher order functions and abbreviations. I do not understand how this part of the code works to return "true".

function greaterThan(n) {
  return function(m) { return m > n; };
}

var greaterThan10 = greaterThan(10);

console.log(greaterThan10(11));

Thanks for the help.

+4
source share
1 answer

The function greaterThanreturns the function when called. The returned function has access to all members of the external function even after the function returns. This is called closure.

function greaterThan(n) {
    return function (m) {
        return m > n;
    };
}

When following the instructions

var greaterThan10 = greaterThan(10);

it is converted as

var greaterThan10 = function (m) {
    return m > 10;
};

So, greaterThan10now it is a function and can be called as

console.log(greaterThan10(11));

m 11, return 11 > 10; true.

:

JavaScript?

, JS

http://dmitryfrank.com/articles/js_closures

+12

Source: https://habr.com/ru/post/1608761/