Advertisement
Guest User

Untitled

a guest
May 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. /* A Higher order function is a function that takes a function as an argument or a function that returns a function as an argument */
  2. //1- a function that accepts a function as an argument
  3. document.addEventListener('click',myFunction);
  4. function myFunction(){
  5. console.log('You clicked ..');
  6. }
  7.  
  8. //2- a function that returns a function as an argument
  9. function MultiplyMe(multiplier){
  10. return function(x){
  11. return x * multiplier;
  12. }
  13. }
  14.  
  15. let doubleMe = MultiplyMe(2);
  16. let tripleMe = MultiplyMe(3);
  17.  
  18. console.log(doubleMe(10));
  19. console.log(tripleMe(5));
  20.  
  21. //3- Useful higher order function examples (that are a part of core js)
  22. // forEach is a function that takes a function as an argument and runs it for each ele in an array
  23. let colors=['red','green','yellow','blue','grey'];
  24. colors.forEach(saySomething);
  25.  
  26. function saySomething(color){
  27. console.log(`${color} is a great color`);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement