Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Last active March 13, 2019 14:25
Show Gist options
  • Save munkacsitomi/ab849f4f243346d323b3888524262fca to your computer and use it in GitHub Desktop.
Save munkacsitomi/ab849f4f243346d323b3888524262fca to your computer and use it in GitHub Desktop.
Factory function examples in JavaScript
// dog example
const dog = () => {
const sound = 'Where are my testicles, Summer?';
return {
talk: () => console.log(sound)
}
}
const snuffles = dog();
snuffles.talk();
// create user example
const createUser = ({
userName = 'Anonymous',
avatar = 'anon.png'
} = {}) => ({
userName,
avatar
});
console.log(
// { userName: "echo", avatar: 'anon.png' }
createUser({ userName: 'echo' }),
// { userName: "Anonymous", avatar: 'anon.png' }
createUser()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment