DEV Community

Discussion on: Higher Order Functions in JS

Collapse
 
loucyx profile image
Lou Cyx

A few things:

  • You should avoid mutations of the native objects prototypes, such as Array. So to any junior reading this: Avoid code like the last example.
  • Your examples are mutating output, ideally that output variable should only live inside calculate.
  • Your first example of HOC could use some modernization, and it actually doesn't need calculate, you can just use the native Array.prototype.map:
const radiuses = [2, 3, 4, 5, 6, 7];

const area = radius => Math.PI * radius ** 2;
const circumference = radius => Math.PI * diameter(radius);
const diameter = radius => 2 * radius;

console.log(radiuses.map(area));
console.log(radiuses.map(circumference));
console.log(radiuses.map(diameter));
Enter fullscreen mode Exit fullscreen mode

Cheers!

Collapse
 
umashankar_s profile image
Umashankar S • Edited

Yes, That is correct. we should not touch the Array prototypes (we should not mutate that).
But Just I'm explaining that how we can create our own functions like maps using the Higher order functions. Yes, we can use the modern arrow functions , instead of normal functions.

Thanks for your suggestions :)

Some comments have been hidden by the post's author - find out more