HTML and CSS Reference
In-Depth Information
Total is 6
Current value is 5
Total is 10
The total value is initially set to the value of the first element in the array. Each member is
then passed to the function, and we add this to the total. At the end, the total is 15, which is
returned from the reduce method.
We can now write our final version of the code with map , filter and reduce chained to-
gether, each using the result of its predecessor as its input:
> {return i*i})
.filter(function(i) {return i % 2 == 0})
.reduce(function(total, currentValue, index, array) { return total += currentValue; })
Functions that accept other functions allow software engineers to write extremely concise
code. In many strongly typed Object Orientated languages, such as Java, functions are not
first class language constructs. In order to write a function (or method), you first must con-
struct a class to contain it, and then an object from that class. Although Java allows an-
onymous classes, the syntax for performing the examples above would be nowhere near as
Search WWH ::

Custom Search