esra-justBI
1/25/2019 - 9:04 AM

High-Order functions

Functions help to make clear, readable programs. Higher-order functions accept other functions as arguments and/or return functions as output.

Functions are first class objects: they can have properties and methods.

Call-back functions: functions that get passed in as parameters and invoked, they get called during the execution of the higher-order function. When we pass a function in as an argument to another function, we don't invoke it. Inovking the function would evaluate to the return value of that function call. With callbacks, we pass in the function itself by typing the function name without the ().

const XXXXXXXXXXX = () => {
...
}

const X = XXXXXXX;
now you can run X(); as if you are running XXXXX(); 

//higher-order functions
const timeFuncRuntime = funcParameter => { // this is the higher-order function, takes function as argument
   let t1 = Date.now();
   funcParameter();
   let t2 = Date.now();
   return t2 - t1;
}

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

timeFuncRuntime(() => { //we invoked timeFuncRuntime() with an anonymous function
  for (let i = 10; i>0; i--){
    console.log(i);
  }
});