First-class higher-order functions

The term first class means that the language itself treats functions as values; they can be passed around as example arguments to other functions. A higher-order function is a function that takes other functions as parameters. Let's look at an example to make this clearer:

function project(obj, fn) {
return fn(obj);
}

project( { name : 'chris', age: 37 }, (obj) => obj['name'] ); // 'chris'
project({ name : 'chris', age: 37 }, (obj) => obj['age'] ) // 37

Here we can see that the second argument in our project() function is a function. The function is being applied to the first argument. We can also see that, depending on what input argument we give the higher-order function as its second argument, the higher-order function will behave differently.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset