Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created July 30, 2018 23:29
Show Gist options
  • Select an option

  • Save gladchinda/530b89c1f989dbbbf3a784a2f90c3ee0 to your computer and use it in GitHub Desktop.

Select an option

Save gladchinda/530b89c1f989dbbbf3a784a2f90c3ee0 to your computer and use it in GitHub Desktop.
// IIFE: With Arrow Function
// The arrow function is called immediately with a list of arguments
// and the return value is assigned to the `compute` variable
const compute = ((...numbers) => {
// Private members
const length = numbers.length;
const min = Math.min(...numbers);
const max = Math.max(...numbers);
const sum = numbers.reduce((a, b) => a + Number(b), 0);
// Expose an inteface of public methods
return {
sum: () => sum,
avg: () => sum / length,
range: () => max - min
};
})(42, 68, 49, 83, 72, 65, 77, 74, 86, 51, 69, 47, 53, 58, 51);
// Access the exposed public methods
console.log(compute.sum()); // 945
console.log(compute.avg()); // 63
console.log(compute.range()); // 44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment