A function can take a function object as an argument. twice takes a function object; it returns a new function that calls that function twice.

Results

function plus1(x) {
    return x+1;
}
function twice(fn) { return function(x) { return fn(fn(x)); } }   var plus2 = twice(plus1); log(plus2(10));