Skip to content

Instantly share code, notes, and snippets.

@Yamo93
Created July 6, 2022 18:24
Show Gist options
  • Save Yamo93/cc6d804c411ab2e0bfa1343d1121e2c8 to your computer and use it in GitHub Desktop.
Save Yamo93/cc6d804c411ab2e0bfa1343d1121e2c8 to your computer and use it in GitHub Desktop.
A chained function with closures.
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