Search code examples
javascriptfunctionhigher-order-functions

I don't understand about higher-order function parameter passed in example


I was reading article about higher-order functions. And they give code example like this:

var humans = function(data) {
  return data.filter(function(character) {
    return character.species === 'human';
  })
}

var images = function(data) {
  return data.map(function(character) {
    return character.img;
  })
}

function compose(func1, func2) {
  return function(data) {
    return func2(func1(data));
  };
}

var displayCharacterImages = compose(humans, images);

The one thing that's I don't understand is where is the data are gonna passed in compose function? I mean we only pass func1, func2 as params. So where can the data go in compose function? I am still a newbie so please help me understand this


Solution

  • Compose function returns a function not a value. So when you call compose you get a function. displayCharacterImages is a function not a value.

    The data variable is the returned functions argument.

    Basically, you will be doing this :
    displayCharacterImages(foo)
    So data will get the value you passed in foo.