Created
July 6, 2022 18:24
-
-
Save Yamo93/cc6d804c411ab2e0bfa1343d1121e2c8 to your computer and use it in GitHub Desktop.
A chained function with closures.
This file contains 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 fn (str) { | |
let appended = str; | |
const stringBuilder = { | |
fn (newStr) { | |
if (typeof newStr === 'string') { | |
appended += ' '; | |
appended += newStr; | |
return stringBuilder; | |
} | |
return appended | |
} | |
}; | |
return stringBuilder; | |
} | |
console.log(fn('hello').fn('world').fn('!!!').fn()) // 'hello world !!!' | |
console.log(fn('This').fn('is').fn('just').fn('a').fn('test').fn()) // 'This is just a test' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment