Can you explain how this piece of code works?

  • 0
    I watched a video on functional programming, in which I encountered this example of a higher-order function:
    function makeAdjectifier(adjective) {
        return function (string) {
            return adjective + " " + string;
        };
    }
    
    var coolifier = makeAdjectifier("cool");
    coolifier("conference");

    Please explain step by step what is happening here. How does conference get into string , and how does cool get into adjective ?
    JavaScript Nora Harrison, Dec 20, 2019

  • 3 Answers
  • 0
    How does conference get into string, and how does cool get into adjective?


    You pass them yourself as function arguments
    Hadley Arias

  • 0
    The coolifier variable becomes a function with a parameter
    Anonymous

  • 0
    The function returns a function. The second function remembers what was passed to the first function. This is a feature of some languages.



    Roughly speaking, all variables and all parameters that were in the first function are written to memory and are stored there forever. And from the second function, you can refer to this "cast".



    The makeAdjectifier function, when called, “injects” the word “cool” into the returned function.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!