Created
May 24, 2019 09:21
-
-
Save ross-u/16791e5c4738870666a696334f600db5 to your computer and use it in GitHub Desktop.
Higher order function examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function outter () { | |
return function(wordToPrint) { | |
console.log('outter()(wordToPrint) -->', wordToPrint) | |
} | |
} | |
// Invoke `outter` and save returned value (function) in a variable | |
const inner = outter(); | |
inner('Hello'); | |
// Or we can do everything in one expression | |
outter()('Bye'); | |
// Same pattern with additional function returning a function | |
function triple () { | |
return function() { | |
return function(wordToPrint) { | |
console.log('triple()()(wordToPrint) -->', wordToPrint) | |
} | |
} | |
} | |
triple()()('hi hi hi') | |
// Same pattern where we pass parameter to each function | |
function doubleWithParams (firstWord) { | |
return function (secondWord) { | |
console.log('doubleWithParams(firstWord)(secondWord) -->', firstWord + secondWord) | |
} | |
} | |
doubleWithParams('Bat')('man'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment