The standard array method reduce has an added convenience. If your array contains at least one element, you are allowed to leave off the start argument. The method will take the first element of the array as its start value and start reducing at the second element.
Binding
The bind method, which all functions have, creates a new function that will call the original function but with some of the arguments already fixed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vartheSet=["Carel Haverbeke","Maria van Brussel","Donald Duck"];functionisInSet(set,person){returnset.indexOf(person.name)>-1;}console.log(ancestry.filter(function(person){returnisInSet(theSet,person);}));// → [{name: "Maria van Brussel", …},
// {name: "Carel Haverbeke", …}]
console.log(ancestry.filter(isInSet.bind(null,theSet)));// → … same result
Summary
Arrays provide a number of useful higher-order methods forEach to do something with each element in an array, filter to build a new array with some elements filtered out, map to build a new array where each element has been put through a function, and reduce to combine all an array’s elements into a single value.
Functions have an apply method that can be used to call them with an array specifying their arguments. They also have a bind method, which is used to create a partially applied version of the function.